Bash Printf Command

Bash Printf Command

The bash printf command prints the formatted strings in the shell. It stands for print formatted.

The command is extensively used in many programming languages like C, C++, Java, etc., in the form of the printf() function. It makes it easier to grasp the concept if you have worked with the printf() function in some programming languages.

In bash, you probably have used the echo command. The echo and the printf commands may seem similar, but they have a big underlying difference.

The echo command has a narrower scope than the printf command. First, let’s explore the contrast between these two commands from the examples below.

echo
echo hey there
echo Output
hey there
printf
printf hey there
printf Output
hey

In the example above, you can notice two differences. The first one is printf prints only the first word, while echo prints the whole sentence. The second difference is that there is an end of the line after the output while using echo, but you cannot find it using printf.

So, to print the whole sentence, you can wrap the sentence with quotes and use a newline character to force the output to the newline.

echo

echo hey there
echo Output
hey there
printf
printf "hey there"
printf Output
hey there

One important thing that you can conclude from the above example is that echo ends the arguments with the end of line character automatically while printf doesn’t.

printf Syntax

The syntax of the printf command looks like this:

printf [-v var] format [arguments]
  • -v var: The shell variable var is assigned with output instead of displaying the output directly on the standard output. The -v flag does this operation.
  • format: It specifies normal character, escape character, or the format specifiers
  • arguments: Arguments are the values pointed by the format specifiers. They get printed out in the standard output.

Escape Sequence

The escape sequence invokes the character that cannot be directly printed in the output. It comprises a backslash() followed by an escape sequence character. Some of the most common escape characters are listed below.

Escape Sequence Behavior
\” displays the quotes
\\ displays a backslash
\n displays a new line
\b performs backspace operation
\t displays horizontal tab
\v displays vertical tab
\? displays question mark

Look at the examples below to see the usage of these various escape sequences.

Example 1
printf "Today is \"Saturday\". How is your weekend going\?\n"
Output
Today is "Saturday". How is your weekend going?
Example 2
printf "let\'s write a backslash \\ \n"
Output
let's write a backslash \
Example 3
printf "writing a \t horizontal tab \n writing a \v vertical tab \n"
Output
writing a horizontal tab
writing a
vertical tab

Format Specifier

The format specifier is the specifier preceded by a % symbol. They resemble the corresponding arguments and provide the formating. The common format specifier is listed below.

Format Specifier Function
%d prints the input/argument as decimal
%s print the input/argument as a string
%c print the input/argument as a character
%f print the input/argument as floating point
%% print the % literal

The examples below demonstrate the usages of these format specifiers.

#!/bin/bash
printf "%s %s\n" "hey" "Tom!"
printf "%c is a character \n" "z"
printf "%d is a decimal number \n" "20"
printf "%2.3f is a floating-point number \n" "22.2"
printf "printing the %% literal\n"
Output
hey Tom!
z is a character
20 is a decimal number
22.200 is a floating-point number
printing the % literal

In the example above, hey and Tom! are strings, so the format specifier %s represents the strings. Likewise, z, a character, is represented by the specifier %c, 20, a decimal number, is represented by the specifier $d.

The floating-point representation looks a little different from the others. The floating-point argument provided is 22.2, and the %2.3f format specifier. Here, 2 represents the width, and 3 represents the precision after the decimal point.

Conversion Specifier

There are certain conversion specifiers in bash that convert arguments from one format to another.

The example below demonstrates converting a decimal number into hexadecimal and octal numbers using respective conversion specifiers.

printf "Decimal: %d\nHexadecimal: %x\nOctal: %o\n" 10 10 10
Output
Decimal: 10
Hexadecimal: a
Octal: 12

In the above example, %x and %o conversion specifiers convert decimal to hexadecimal and octal numbers.

Some Examples of printf

There are a few examples of using the printf command in the section below.

Printing User Input Values

In bash, there is the provision of reading the input from users, saving it in a variable, and printing it out, just like in programming languages. The example is shown below.

#!/bin/bash
printf "Enter your name\n"
read name
printf "Enter your age\n"
read age
printf "hi my name is $name and I am $age years old\n"
Output
hi my name is subodh, and I am 23 years old

In the example above, the read command asks for the input from the user and saves the values in the name and age variables. In the printf command, the variables are written with a preceding $ sign so that the values can be interpreted.

Printing Thousands with Comma

The following example demonstrates how you can add a comma to thousands in a number. To achieve it, the apostrophe ' sign should be used after the % symbol and before d in the specifier %d.

printf "%'d\n" 9999999
Output
9,999,999

Printing Expression

The example below shows how to print arithmetic expressions with the result in bash.

printf "32+18=%d\n" $((32+18))
Output
32+18=50

Here, the expression 32+18= is written inside the quotes and printed out the way it is. The %d specifier represents the expression $((32+18)), which evaluates to 50, as shown in the output.

Conclusion

In this tutorial, you learned about the printf command, its syntax, the format specifiers, and the escape sequences with practical implementation.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Check File If Exists
Read More

Bash Check If File Exists

When working with files in bash, it is essential to know whether the particular file or directory exists.…