Site icon DesignLinux

Bash printf Command

Bash printf

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:

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:

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:

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:

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:

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.

Exit mobile version