|Contents||Bulletin||Scripting in shell and Perl||Network troubleshooting||History||Humor|
The built-in printf (print formatted) command prints a message to the screen. This is upgraded version of the command echo that existed in shell since the very beginning. Command printf provides better control over output then echo. Should be used instead echo. Echo can be emulated using alias or function.
alias echo='printf "%s\n" '
Bash 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 5 $ printf "%f\n" 5 5.000000
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:] WARNING: THE FILES FROM THE OHIO OFFICE HAVEN'T ARRIVED. $ tr [:lower:] [:upper:] <<< "$ERRMSG" WARNING: THE FILES FROM THE OHIO OFFICE HAVEN'T ARRIVED.
The format codes include the following.
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:
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 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 17.20000
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.
$ printf "Two separate\nlines\n" Two separate lines
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 8 $ printf "%d\n " 0xF 15 $ printf "0x%X\n " 15 0xF $ printf "0%o\n " 8 010
Most Linux distributions also have a separate printf command to be compliant with the POSIX standard.