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.
echo command has a narrower scope than the
printf command. First, let’s explore the contrast between these two commands from the examples below.
Table of Contents
echo hey there
printf hey there
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
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 hey there
printf "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
The syntax of the
printf command looks like this:
printf [-v var] format [arguments]
-v var: The shell variable
varis assigned with output instead of displaying the output directly on the standard output. The
-vflag 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.
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.
|\”||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.
printf "Today is \"Saturday\". How is your weekend going\?\n"
Today is "Saturday". How is your weekend going?
printf "let\'s write a backslash \\ \n"
let's write a backslash \
printf "writing a \t horizontal tab \n writing a \v vertical tab \n"
writing a horizontal tab writing a vertical tab
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.
|%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"
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,
Tom! are strings, so the format specifier
%s represents the strings. Likewise,
z, a character, is represented by the specifier
20, a decimal number, is represented by the specifier
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.
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
Decimal: 10 Hexadecimal: a Octal: 12
In the above example,
%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"
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
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
printf "%'d\n" 9999999
The example below shows how to print arithmetic expressions with the result in bash.
printf "32+18=%d\n" $((32+18))
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.
In this tutorial, you learned about the
printf command, its syntax, the format specifiers, and the escape sequences with practical implementation.