Write formatted output (POSIX)
printf format [argument...]
Neutrino
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:
Introduce a conversion specification by the % character. To literally display this character, 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 from 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 | 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 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:
[-]d[.ddd]e{+|-}dd
The first part is an optional sign. The second is a single digit, which is nonzero if the argument is nonzero. The third (optional) part — a decimal character followed by a number of digits equal to the specified precision — is printed if the precision is nonzero. If the precision isn't specified, it's assumed to be 6. If the precision is 0, the decimal isn't printed. The fourth part is an e or E, based upon the case of the conversion character, followed by a signed value. This value represents the order of magnitude.
[-]ddd.ddd
where the number of digits printed after the decimal character is equal to the specified precision. If the precision isn't specified, it's assumed to be 6. If the precision is 0, the decimal isn't printed.
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