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.
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 [-v var] format [arguments]
-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
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
- The shell will consider all variables, wildcard matching, and special characters after passing the arguments to the
- 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 "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
"24" are arguments. There are two newline characters (
\n) and two format specifiers (
%s) that are replaced with the arguments.
printf command doesn’t add a newline character (
\n) at the end of the line.
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.
Below is the form of conversion specification:
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:
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.
%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– Print the argument as an unsigned hexadecimal integer.
%xprints lower-case letters and
%E– Print the argument as a floating-point number in exponential notation.
%A– Print the argument as a floating-point number in hexadecimal fractional notation.
%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
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 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 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 (
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 (
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.