1. Overview
Sometimes we would like to format the output of a Linux command in a certain way. Perhaps we need to avoid glutted output and display the content in a simple and easy-to-read format.
In this quick tutorial, we’ll look at echo and printf commands on Linux and Unix-based systems for formatting the output of shell script commands.
2. printf
printf command is used to output a given string, number or any other format specifier. The command operates the same way as printf in C, C++, and Java programming languages.
Let’s look at different instances where we can use printf.
2.1. Printing Text
We can use printf command to display the text to the standard output stream.
Let’s print the welcoming message:
$ printf "Welcome to the Baeldung website"
Welcome to the Baeldung website[Admin@admin ~]$
The output of the command and the next command prompt are on the same line. No newline is printed.
2.2. Formatting Output With String
We can use the printf command with %s to print the string value. The %s is the format specifier that we use to output the string:
$ printf "%s" "Hello, Welcome to Baeldung"
Hello, Welcome to Baeldung[Admin@admin ~]$
The output displays the string value of the input text.
2.3. Formatting Output With New Line
Running printf with format specifiers will display the output and the command prompt on the same line. To print a new line, we use \n escape sequence:
$ printf "Welcome to Baeldung \n"
Welcome to Baeldung
[Admin@admin ~]$
The expected output is printed and the next command prompt is moved to the next line.
2.4. Printing Integer Values
We can use printf to format strings with an integral value using %d specifier:
$ printf "%d\n" "1234"
1234
[Admin@admin ~]$
The output is an integer value.
2.5. Printing Float Values
Using %f format specifier with printf can help us to print the floating-point values:
$ printf "%f\n" "20.20"
20.200000
[Admin@admin ~]$
The command displays the float value of the input string.
2.6. Printing Environmental Variable
Sometimes we want to know the value of a specific environmental variable, printf can help us achieve such. Let’s print the shell that we are currently running on:
$ printf "The shell is: "$SHELL
The shell is: /bin/bash[Admin@admin ~]$
From the output, the current shell is Bash.
2.7. Printing Date and Time
We can use printf command with format specifiers for a month, day, year, hour, minute, and second to print date and time. The output shows the current month and year since we passed the month and year format specifiers only:
$ printf "%(%m-%Y)T" $(date +%s)
07-2021[Admin@admin ~]$
This gives us the month and year plus the current time in hours and minutes:
$ printf "%(%m-%Y %H:%M)T" $(date +%s)
07-2021 23:24[Admin@admin ~]$
2.8. Formatting Hexadecimal Values
We can use a %X specifier to output hexadecimal of integral values in uppercase:
$ printf %X"\n" 450
1C2
[Admin@admin ~]$
On the other hand, we can use a %x to print in lowercase:
$ printf %x"\n" 450
1c2
[Admin@admin ~]$
This is the equivalent hexadecimal value of the number 450.
3. echo
echo command is one of the most used commands in Linux shell scripts. It is used to display a line of text or strings on standard output or a file.
We’ll begin to look at various ways in which the command is used:
3.1. Printing Text to the Standard Output
echo command is used to print a string from the standard input to the standard output:
$ echo "Welcome to the Baeldung"
Welcome to the Baeldung
[Admin@admin~]$
From the result, the input string is displayed in our standard output. Unlike printf, echo command creates a new line implicitly.
3.2. Printing of Variable Values
We can declare a variable value in the terminal and output it on the standard output using the echo command:
[Admin@admin ~]$ num=45
[Admin@admin ~]$ echo $num
45
[Admin@admin ~]$
For example, we declared a variable called num and assigned it to a value of 46. We then printed the value using echo which gives us the expected output.
It’s important to note that the variable will exist for this session of the terminal only.
3.3. Creating New Line
To create a new line using the echo command, we use \n option. However, the \n option won’t work and we need to combine it with an -e option to escape characters:
[Admin@admin ~]$ echo -e "Baeldung \nLinux"
Baeldung
Linux
[Admin@admin ~]$
The output will be two lines which resulted from creating the new line before the second word Linux.
3.4. Creating Horizontal Tab
We can create tab spaces between words in the input string. We use the escape character with \t option:
[Admin@admin ~]$ echo -e "Welcome\t to \t Linux"
Welcome to Linux
[Admin@admin ~]$
From the output, we can see that there is an extra tab space created.
3.5. Removing Spaces Between Text
We can remove spaces between words using -e parameter and \b option:
[Admin@admin ~]$ echo -e "Welcome \bto \bLinux"
WelcometoLinux
[Admin@admin ~]$
We successfully removed the spaces between the words.
3.6. Creating Vertical Tab Spaces Between Text
If we use -e parameter and \v option, it’ll create a vertical tab between words in the input string:
[Admin@admin~]$ echo -e "Welcome \vto \vLinux"
Welcome
to
Linux
[Admin@admin ~]$
The output of the command creates vertical tabs between the words of the input string.
3.7. Printing All Files and Folders
We can print all files and folders in the current working directory using echo and *:
[Admin@admin Documents]$ echo *
Classical LinuxWork Microservices postgresSampleDb Print Smart_Java.pdf Springboot Untitled 2.pdf wildfly-21.0.1.Final.zip
[Admin@admin Documents]$
From the output, we can see that we printed all the folders and files in the Documents directory.
4. Conclusion
In this tutorial, we have seen the usage of echo and printf commands in Linux and Unix-based systems. We also saw the capabilities of both commands. From the usage and capabilities, it is worth noting that echo sends a new line at the end of its output, unlike printf which you have to explicitly use the \n option to create a new line.