PRINTF(1) User Commands PRINTF(1)

printf - print a text string

printf format [argument ...]

The printf command writes a string to standard output with a controlled format. It is essentially a utility variant of the `C' language function printf(3).

Most characters in the format string are simply copied. The only exceptions are the backslash `\' and percent `%' characters.

The backslash `\' character in the format string serves as an escape for the following character. The sequences `\a' (alert), `\b' (backspace), `\f' (formfeed), `\n' (newline), `\r' (carriage return), `\t' (tabulator), and `\v' (vertical tabulator) cause the corresponding control characters to be printed. The sequence `\nnn', where nnn is a one-, two-, or three-digit octal number, cause the character (byte) with the corresponding number to be printed. `\\' prints the backslash character itself.

The percent `%' character in the format string introduces a conversion specification consisting of up to five parts, of which the first four are optional:

A positive decimal integer followed by a dollar `$' character that specifies the argument to be used instead of the next unused one for the following conversions.
Zero or more of
-
left-justify the field
+
always include a sign when printing a number
<space>
prefix the result by a space if there is no sign
#
alternate format
0
pad numbers with leading zeros
A decimal number giving the minimum width of the output printed. The output is padded if necessary, as controlled by the flags described above. By default, no padding is performed. If the field width is `*', it is taken from the next unused argument.
A dot `.', followed by a decimal number giving the minimum digits written for integer numeric conversions, the minumum digits after the radix character for floating-point numeric conversions, or maximum bytes written for string conversions. If the precisision is `.*', it is taken from the next unused argument.
A character controlling the type of the conversion performed:
The next unused argument is written as a plain string.
The next unused argument is written as a string containing escape sequences similar to those of echo(1). The backslash sequences described above are supported, except that the octal number must be prefixed by a zero as in `\0nnn'. A `\c' sequence causes printf to exit immediately when it is encountered.
The first character (byte) of the next unused argument is written. Any following characters in this argument are ignored.
The next unused argument is written as a signed decimal number.
The next unused argument is written as an octal number. With the `#' flag, it is prefixed by `0'.
The next unused argument is written as an unsigned decimal number.
The next unused argument is written as a hexadecimal number, using lowercase characters. With the `#' flag, it is prefixed by `0x'.
The next unused argument is written as a hexadecimal number, using uppercase characters. With the `#' flag, it is prefixed by `0X'.
The next unused argument is written as a floating-point number in the style `[-]nnn.nnn'.
The next unused argument is written as a floating-point number in the style `[-]n.nnne[+|-]nn'.
The next unused argument is written as a floating-point number in the style `[-]n.nnnE[+|-]nn'.
The next unused argument is written as a floating-point number like an integer if there is no fractional part, as described for `f' if the exponent is small, or as described for `g' if the exponent is large.
The next unused argument is written as a floating-point number like an integer if there is no fractional part, as described for `f' if the exponent is small, or as described for `G' if the exponent is large.
%
A percent character is printed. No argument is consumed.

If the argument for the numeric specifiers starts with a single- or double quote (`'c' or `"c'), the numeric value of the following character (byte sequence) in the current character encoding is used.

If the format string consumes at least an argument, no format specification contains a position part, but there are still unused arguments after the entire format string has been evaluated once, it is evaluated repeatedly until all arguments are consumed. Missing arguments default to the empty string for string conversions, and to zero if a numeric value is expected.

See locale(7).
Determines the mapping of bytes to characters for `'c' and `"c'.

echo(1), printf(3)

7/17/05 Heirloom Toolchest