The printf command, available in bash since version 2.02, has two parts (beyond the command name): a format string and a variable number of arguments:
printfformat-string [arguments]
format-string
describes the format
specifications; this is best supplied as a string constant in quotes.
arguments
is a list, such as a list of strings
or variable values that correspond to the format specifications.
The format is reused as necessary to use up all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied.
A format specification is preceded by a percent sign (%), and the specifier is one of the characters described below. Two of the main format specifiers are %s for strings and %d for decimal integers (see Table A-13).
Table A-13. printf format specifiers
Format character | Meaning |
---|---|
| ASCII character (prints first character of corresponding argument) |
| Decimal (base 10) integer |
| Floating-point format ( |
| Floating-point format ( |
| Floating-point format ( |
|
|
|
|
| Unsigned octal value |
| String |
| Unsigned decimal value |
| Unsigned hexadecimal number; uses |
| Unsigned hexadecimal number; uses |
%% | Literal % |
The printf command can be used to specify the width and alignment of output fields. A format expression can take three optional modifiers following % and preceding the format specifier:
%flags width.precision format-specifier
The width of the output field is a numeric value. When you specify a
field width, the contents of the field are right-justified by default. You
must specify a flag of -to get left-justification (the rest of the flags
are shown in the table). Thus, %-20s
outputs a left-justified string in a field 20-characters wide. If the
string is less than 20 characters, the field is padded with whitespace to
fill. In the following examples, we put our format specifier between a
pair of | in our format string so you can see the width of the field in
the output. The first example right-justifies the text:
printf "|%10s| " hello@
It produces:
| hello|
The next example left-justifies the text:
printf "|%-10s| " hello
It produces:
|hello |
The precision modifier, used for decimal or floating-point values, controls the number of digits that appear in the result. For string values, it controls the maximum number of characters from the string that will be printed.
You can even specify both the width and precision dynamically, via values in the printf argument list. You do this by specifying asterisks in the format expression, instead of literal values:
$ myvar=42.123456 $ mysig=6 $ printf "|%*.*G| " 5 $mysig $myvar |42.1235|
In this example, the width is 5, the precision is 6, and the value
to print comes from the value of $myvar
. The precision is optional and its exact
meaning varies by control letter, as shown in Table A-14.
Table A-14. Meaning of “precision” based on printf format specifier
Format | What “precision” means |
---|---|
| The minimum number of digits to print. When the value has fewer digits, it is padded with leading zeros. The default precision is 1. |
| The minimum number of digits to print. When the value has fewer digits, it is padded with zeros after the decimal point. The default precision is 10. A precision of 0 inhibits printing of the decimal point. |
| The number of digits to the right of the decimal point. |
| The maximum number of significant digits. |
| The maximum number of characters to print. |
| [POSIX Shell—may be nonportable to other versions of
printf.] When used instead of |
| [POSIX Shell—may be nonportable to other versions of
printf.] When used instead of |
%b
and %q
are additions to bash
(and other POSIX compliant shells) which provide useful features at the
expense of nonportability to versions of the printf
command found in some other shells and in other places in Unix. Here are
two examples to make their functions a little clearer:
%q
shell quotes:
$ printf "%q " "greetings to the world" greetings to the world
%b
echo-style escapes:
$ printf "%s " 'hello world' hello world $ printf "%b " 'hello world' hello world
Table A-15 shows the escape
sequences that will be translated in a string printed with the %b
format.
Table A-15. printf escape sequences
Escape sequence | Meaning |
---|---|
| Escape character |
| Bell character |
| Backspace character |
| Form-feed character |
| Newline character |
| Carriage return character |
| Tab character |
| Vertical tab character |
' | Single-quote character |
” | Double-quote character |
\ | Backslash character |
| 8-bit character whose ASCII value is the 1, 2, or 3
digit octal number |
| 8-bit character whose ASCII value is the 1 or 2 digit
hexadecimal number |
Finally, one or more flags may precede the field width and the precision in a printf format specifier. We’ve already seen the -flag for left-justification. The rest of the flags are shown in Table A-16.
Table A-16. printf flags
Character | Description |
---|---|
- | Left-justify the formatted value within the field. |
| Prefix positive values with a space and negative values with a minus. |
+ | Always prefix numeric values with a sign, even if the value is positive. |
# | Use an alternate form: |
| Pad output with zeros, not spaces. This only happens when the field width is wider than the converted result. In the C language, this flag applies to all output formats, even non-numeric ones. For bash, it only applies to the numeric formats. |
' | Format with thousands’ grouping characters if
|
These examples for printf use some shell variables, assigned as follows in Table A-17:
PI=3.141592653589
Table A-17. printf examples
printf statement | Result | Comment |
---|---|---|
| 3.141593 | Note the default rounding. |
| 3.14.5 | A common mistake—the format specifier should be on
the other side of the |
| 3.14159 | Gives five places to the right of the decimal point. |
| +3.14 | Leading + sign, only two digits to the right of the decimal point. |
| [s] [stri] | Truncates to four characters; with only one character, we get only one character-wide output, note reuse of format string. |
| [s] [string] | Assures us of a minimum four-character field width, right-justified; doesn’t truncate, though. |
| [s] [stri] | Does it all—minimum width of four, maximum width of four, truncating if necessary, and left justifies (due to the minus sign) shorter than four. |
Here is one more example that will not display well in the table. The traditional way to write printf statements is to embed all formatting, including things like newlines, in the format string. This is shown in the table. That is encouraged, but you don’t have to do it that way, and sometimes it’s easier if you don’t. Note the →denotes a Tab character in the output:
$ printf "%b" "aRing terminal bell, then tab then newline Then line 2. " Ring terminal bell, then tab → then newline Then line 2.
3.17.76.175