printf |
---|
|
printf format [ string ... ] |
---|
|
To produce output from shell scripts. Since printf's behavior is defined by the POSIX standard, scripts that use it can be more portable than those that use echo. |
---|
|
None. |
---|
|
printf uses the format string to control the output. Plain characters in the string are printed. Escape sequences as described for echo are interpreted. Format specifiers consisting of % and a letter direct formatting of corresponding argument strings. See text for details. |
---|
|
Sequence | Description
\a
| Alert character, usually the ASCII BEL character.
| \b
| Backspace.
| \c
| Suppress any final newline in the output.[*] Furthermore, any
characters
left in the argument, any following arguments, and any
characters left in the
format string are ignored (not printed).
| \f
| Formfeed.
| \n
| Newline.
| \r
| Carriage return.
| \t
| Horizontal tab.
| \v
| Vertical tab.
| \\
| A literal backslash character.
| \ddd
| Character represented as a 1- to 3-digit octal value. Valid
only in the
format string.
| \0ddd
| Character represented as a 1- to 3-digit octal value.
| |
---|
[*] Valid only in argument strings under control of the %b format specifier. |
---|
Confusion of Printf Escape Sequences |
---|
By default, escape sequences are treated specially only in the format string. |
---|
Escape sequences appearing in argument strings are not interpreted:
|
---|
But, when the %b format specifier is used, printf does interpret
escape sequences in argument strings:
|
---|
\c and \0ddd are only valid for use with %b, |
---|
\ddd is only interpreted in the format string. |
---|
|
Item | Description
%b
| The corresponding argument is treated as a string
containing escape sequences to be processed.
| %c
| ASCII character. Print the first character of the
corresponding argument.
| %d, %i
| Decimal integer.
| %e
| Floating-point format ([-]d.precisione[+-]dd).
| %E
| Floating-point format ([-]d.precisionE[+-]dd).
| %f
| Floating-point format ([-]ddd.precision).
| %g
| %e or %f conversion, whichever is shorter, with trailing
zeros removed.
| %G
| %E or %f conversion, whichever is shorter, with trailing
zeros removed.
| %o
| Unsigned octal value.
| %s
| String.
| %u
| Unsigned decimal value.
| %x
| Unsigned hexadecimal number. Use a-f for 10 to 15.
| %X
| Unsigned hexadecimal number. Use A-F for 10 to 15.
| %%
| Literal %.
| |
---|
floating-point formats, %e, %E, %f, %g, and %G, are optional in POSIX standard. |
---|
|
To specify the width and alignment of output fields. |
---|
three optional modifiers following the % and preceding the format
specifier:
|
---|
the contents of the field are right-justified by default. |
---|
use flag '-' to change to left justification.
|
---|
Example: right-justify |
---|
|
The precision modifier is optional. |
---|
For decimal or floating-point values, it 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. |
---|
Meaning of precision |
---|
Conversion | Precision means
%d, %i, %o, %u, %x, %X
| The minimum number of digits to print.
| When the value has fewer digits, it is padded with leading zeros. The default precision is 1. %e, %E
| 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 6. A precision of 0 inhibits printing of the decimal point. %f
| The number of digits to the right of the decimal point.
| %g, %G
| The maximum number of significant digits.
| %s
| The maximum number of characters to print.
| |
---|
Examples of the precision in action
|
---|
The C library printf( ) function allows you to specify the width
and precision dynamically, via additional values in the argument
list.
The POSIX standard doesn't supply this, instead recommending the use of shell variable values in the format string. Example:
|
---|
|
one or more flags may precede the field width and the precision. |
Flags for printf
|
---|
Speical Treatment of %b, %c, and %s conversion specifiers
|
---|
When there are more arguments than format specifiers,
|
---|
If there are more specifiers left in the format string than arguments,
|
---|
If printf cannot perform a format conversion,
|
---|