Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

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[[email protected] ~]$ 

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[[email protected] ~]$  

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 
[[email protected] ~]$ 

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"
[[email protected] ~]$ 

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"
[[email protected] ~]$ 

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[[email protected] ~]$ 

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[[email protected] ~]$ 

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[[email protected] ~]$ 

2.8. Formatting Hexadecimal Values

We can use a %X specifier to output hexadecimal of integral values in uppercase:

$ printf %X"\n" 450
[[email protected] ~]$ 

On the other hand, we can use a %x to print in lowercase:

$ printf %x"\n" 450
[[email protected] ~]$ 

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
[[email protected]~]$ 

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:

[[email protected] ~]$ num=45
[[email protected] ~]$ echo $num
[[email protected] ~]$ 

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:

[[email protected] ~]$ echo -e "Baeldung \nLinux"
[[email protected] ~]$ 

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:

[[email protected] ~]$ echo -e "Welcome\t to \t Linux"
Welcome	 to 	 Linux
[[email protected] ~]$ 

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:

[[email protected] ~]$ echo -e "Welcome \bto \bLinux"
[[email protected] ~]$

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:

[[email protected]~]$ echo -e "Welcome \vto \vLinux"
[[email protected] ~]$ 

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 *:

[[email protected] Documents]$ echo *
Classical LinuxWork Microservices postgresSampleDb Print Smart_Java.pdf Springboot Untitled 2.pdf 
[[email protected] 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.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!