Write formatted output (POSIX)
printf format [argument...]
The printf utility writes formatted arguments to standard output under control of the format control string (the syntax for format is based upon the printf() function).
The format control string contains the following types of characters:
You introduce a conversion specification by the % character. To literally display this character, you encode it as %%. Immediately following the % character, you may specify any of the following, in order:
<flags><width><precision><type len><conversion>
where:
These components are processed left to right.
The format control flags modify the meaning of the conversion specification. You can specify zero or more of these flags, in any order:
For this conversion: | The result is formatted as follows: |
---|---|
o (octal) | the first digit is zero |
X or x (hex) | a nonzero result has 0X or 0x prefixed to it |
e, E, f, g, and G | the result always has a decimal point (.), even if no digits follow |
g and G | trailing zeros aren't removed from the result as they usually are |
c, d, i, s, and u | no effect; the # is ignored |
0 (zero) | For all conversions, pad the field width with leading zeros. The 0 flag is ignored for the d, i, o, u, X, and x conversions if a precision is specified; it's also ignored if the - flag is given. |
An optional decimal integer that specifies the minimum number of characters for displaying the formatted item. If no field width is specified, or if the value is less than the number of characters required to represent the converted value (subject to the specified precision), a field of sufficient width to contain the converted value is used.
If the number of characters required to represent the converted value is less than the specified field width, the value is padded on the left (or on the right if the - format control flag is given) with spaces or "0" (zero) characters. If the field width begins with a zero, the value is padded with zeros, otherwise spaces.
An optional decimal integer following a period character (.). The effect of the given precision depends on the conversion character you specify (see "Conversion character," below).
An optional character that modifies the interpreted size of the conversion character. The only supported type length character is l ("el"), which causes a d, i, o, u, X, or x conversion to process a long int or unsigned long int argument.
A character that determines the type of conversion to be applied. For example, if d is specified, printf treats the corresponding argument as a decimal integer. The conversion characters are:
The printf utility interprets the character escape sequences within the format string in a way similar to the C programming language. These sequences, which you introduce via the backslash character (\), are translated as follows:
This sequence: | Writes: |
---|---|
\a | an alert character (bell) |
\b | a backspace character |
\f | a form-feed character |
\n | a newline character |
\r | a carriage return character |
\t | a tab character |
\v | a vertical tab character |
\' | a single quote (') character |
\\ | a backslash (\) character |
\d | the character specified by the one-, two-, or three-digit octal number d |
Display the string "hello, world" on a line by itself:
printf "hello, world\n"
Do the same as above, but don't output a newline:
printf "hello, world"
Display a value in scientific notation:
printf "n = %e\n" 3.1415926535897