Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

printf Command

The built-in printf (print formatted) command prints a message to the screen. You will use this command a lot in shell scripts.

printf is very similar to the C standard I/O printf() function, but they are not identical. In particular, single- and double-quoted strings are treated differently in shell scripts than in C programs.

The first parameter is a format string describing how the items being printed will be represented. For example, the special formatting code "%d" represents an integer number, and the code "%f" represents a floating-point number.

$ printf "%d\n" 5
$ printf "%f\n" 5

Include a format code for each item you want to print. Each format code is replaced with the appropriate value when printed. Any characters in the format string that are not part of a formatting instruction are treated as printable characters.

$ printf "There are %d customers with purchases over %d.\n" 50 20000
There are 50 customers with purchases over 20000.

printf is sometimes used to redirect a variable or some unchanging input to a command. For example, suppose all you want to do is pipe a variable to a command. Instead of using printf, Bash provides a shortcut <<< redirection operator. <<< redirects a string into a command as if it were piped using printf.

The tr command can convert text to uppercase. This example shows an error message being converted to uppercase with both printf and <<<.

$ printf "%s\n" "$ERRMSG" | tr [:lower:] [:upper:]
$ tr [:lower:] [:upper:] <<< "$ERRMSG"

The format codes include the following.

  • %a—Represent a floating-point number in hexadecimal format, using lowercase letters

  • %A—Represent a floating point number in hexadecimal format, using uppercase letters

  • %b—Expand backslash sequences

  • %c—Represent a single character

  • %d—Display a signed number

  • %e—Display a floating-point number, shown in exponential (also called "scientific") notation

  • %f (or %F)—Display a floating-point number without exponential notation

  • %g—(General) Let Bash choose %e or %f, depending on the value

  • %i—Same as %d

  • %0—Display an octal number

  • %q—Quote a string so it can be read properly by a shell script

  • %s—Display an unquoted string

  • %u—Display an unsigned number

  • %x—Display an unsigned hexadecimal number, using lowercase letters

  • %X—Display an unsigned hexadecimal number, using uppercase letters

  • %%—Display a percent sign

If a number is too large, Bash reports an out-of-range error.

$ printf "%d\n" 123456789123456789012
bash: printf: warning: 123456789123456789012: Numerical result out of range

For compatibility with C's printf, Bash also recognizes the following flags, but treats them the same as %d:

  • %j—A C intmax_t or uintmax_t integer

  • %t—A C ptrdiff_t integer

  • %z—A C size_t or ssize_t integer

Also for C compatibility, you can preface the format codes with a l or L to indicate a long number.

The %q format is important in shell script programming and it is discussed in the quoting section, in the Chapter 5, "Variables."

To create reports with neat columns, numbers can proceed many of the formatting codes to indicate the width of a column. For example, "%10d" prints a signed number in a column 10 characters wide.

$ printf "%10d\n" 11

Likewise, a negative number left-justifies the columns.

$ printf "%-10d %-10d\n" 11 12
11     12

A number with a decimal point represents a column width and a minimum number of digits (or decimal places with floating-point values). For example, "%10.5f" indicates a floating-point number in a 10-character column with a minimum of five decimal places.

$ printf "%10.5f\n" 17.2

Finally, an apostrophe (')displays the number with thousands groupings based on the current country locale.

The \n in the format string is an example of a backslash code for representing unprintable characters. \n indicates a new line should be started. There are special backslash formatting codes for the representation of unprintable characters.

  • \b—Backspace

  • \f—Form feed (that is, eject a page on a printer)

  • \n—Start a new line

  • \r—Carriage return

  • \t—Tab

  • \v—Vertical tab

  • \'—Single quote character (for compatibility with C)

  • \\—Backslash

  • \0nn is an octal number representing an 8-bit ASCII character

$ printf "Two separate\nlines\n"
Two separate

Any 8-bit byte or ASCII character can be represented by \0 or \ and its octal value.

$ printf "ASCII 65 (octal 101) is the character \0101\n"
ASCII 65 (octal 101) is the character A

printf recognizes numbers beginning with a zero as octal notation, and numbers beginning with 0x as hexadecimal notation. As a result, printf can convert numbers between these different notations.

$ printf "%d\n" 010
$ printf "%d\n " 0xF
$ printf "0x%X\n " 15
$ printf "0%o\n " 8

Most Linux distributions also have a separate printf command to be compliant with the POSIX standard.

  • + Share This
  • 🔖 Save To Your Account