The printf command have more control over the formatting of the output. It formats and prints arguments similar to the C printf()
function. In this article we will see how to use printf command bash.
printf Command
Bash and other shells like Zsh and Ksh have printf
by default. The standalone binary /usr/bin/printf
is also available but shell built-in version is most preferred. Here, we will see the built-in version of printf
Following is the basic syntax of the printf
command:
printf [-v var] format [arguments]
The -v
option used to assign output to a variable instead of print the output.
In format you can pass string with following three types of objects:
- Normal characters.
- Backslash-escaped characters.
- Conversion specifications
You can pass the n
number of arguments
. The format
specifier will reuse if passed more arguments and consume the all arguments. If passed less arguments
then it will supply the extra numeric-format are set to zero value while string-format specifiers are set to null string.
Below points are useful while passing arguments to the printf
command:
- The shell will consider all variables, wildcard matching, and special characters after passing the arguments to the
printf
command. - When using single quotes
''
the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded. - Following is the common example of using
printf
printf "Pending Complaints: %s\nSolved Complaints: %s\n" "14" "24"
Pending Complaints: 14
Solved Complaints: 24
Here, the text Pending Complaints: %s\nSolved Complaints: %s\n
is the format while "14"
and "24"
are arguments. There are two newline characters (\n
) and two format specifiers (%s
) that are replaced with the arguments.
The printf
command doesn’t add a newline character (\n
) at the end of the line.
Backslash-escaped Characters
The backslash-escaped characters are interpreted when used in the format string or in an argument corresponding to a %b
conversion specifier. Below is the list of the most common escape characters:
\
– To show a backslash character.\b
– Displays a backspace character.\n
– Show a new line.\r
– To display a carriage return.\t
– Displays a horizontal tab.\v
– Used to show a vertical tab.
Conversion specifications
Below is the form of conversion specification:
%[flags][width][.precision]specifier
Every conversion specification stars with the percent sign (%
). It includes optional modifiers and ends with letters that represent the data type (specifier
) of the corresponding argument: aAbcdeEfgGioqsuxX
.
Type conversion specifier
To determine how to interpret the corresponding argument, need to use the type conversion specifier. It’s mendatory and must placed after the optional fields.
Below is the list of all type of conversions with it’s function:
%b
– To print the argument while expanding backslash escape sequences.%q
– Used to print the argument shell-quoted, reusable as input.%d
,%i
– Print the argument as a signed decimal integer.%u
– Use to print the argument as an unsigned decimal integer.%o
– It prints the argument as an unsigned octal integer.%x
,%X
– Print the argument as an unsigned hexadecimal integer.%x
prints lower-case letters and%X
prints upper-case.%e
,%E
– Print the argument as a floating-point number in exponential notation.%a
,%A
– Print the argument as a floating-point number in hexadecimal fractional notation.%g
,%G
– Print the argument as a floating-point number in normal .%c
– Prints the argument as a single character.%f
– It will print the argument as a floating-point number.%s
– Print the argument as a string.%%
– Print a literal%
symbol.
An unsigned number represents zero and positive numbers, while a signed number represents negative, zero, and positive numbers.
The following command prints the number 50
in three different number systems:
printf "Decimal: %d\nHex: %x\nOctal: %o\n" 50 50 50
Decimal: 50
Hex: 32
Octal: 62
Flags directive
Flags are used to set the leading zeros, prefixes, justification, etc. It’s an optional modifier.
Below are most used directives:
-
– Used to align the text to left align. By default, the text is right-aligned.+
– It will prefix the numbers with a + or – signs. Negative numbers are prefixed with a negative sign by default.0
– To pads numbers with leading zeros instead of space.blank
– Prefix the positive numbers with a blank space and negative numbers with a minus (-).#
– An alternative format for numbers.
Width directive
The width
directive used after the flag characters to specify the minimum number of characters the conversion should result in.
If the width of the output text is less than the specified width, it will be padded with spaces. You can specify the width as a non-negative decimal integer or an asterisk (*
).
For example:
printf "%15s %d\n" Kunj 908
In above example, the %15s means set the field at least 15 characters long. It will add the blanks the text because, by default, the output is right-justified. To align the text to left, use the – flag (%-15s
).
Kunj 908
Conclusion
You learned how to use the printf
command to print the formatted text.
If you have any questions or feedback, feel free to leave a comment.