printf - print a text string
Synopsis
Description
Environment Variables
See Also
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:
position 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. flags 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 field width 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. precision 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. specifier A character controlling the type of the conversion performed:
s The next unused argument is written as a plain string. b 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. c The first character (byte) of the next unused argument is written. Any following characters in this argument are ignored. d,i The next unused argument is written as a signed decimal number. o The next unused argument is written as an octal number. With the '#' flag, it is prefixed by '0'. u The next unused argument is written as an unsigned decimal number. x The next unused argument is written as a hexadecimal number, using lowercase characters. With the '#' flag, it is prefixed by '0x'. X The next unused argument is written as a hexadecimal number, using uppercase characters. With the '#' flag, it is prefixed by '0X'. f The next unused argument is written as a floating-point number in the style '[-]nnn.nnn'. e The next unused argument is written as a floating-point number in the style '[-]n.nnne[+|-]nn'. E The next unused argument is written as a floating-point number in the style '[-]n.nnnE[+|-]nn'. g 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. G 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.
LANG, LC_ALL See locale(7). LC_CTYPE Determines the mapping of bytes to characters for ''c' and '"c'.
echo(1), printf(3)
Heirloom Toolchest | PRINTF (1) | 7/17/05 |