Content-type: text/html
printf - Writes formatted output
printf format [argument...]
Interfaces documented on this reference page conform to industry standards as follows:
printf: XPG4, XPG4-UNIX
Refer to the
standards(5)
reference page for more information
about industry standards and associated tags.
A string describing the manner of writing the output. This
string is explained in the
DESCRIPTION
section.
The strings to be written under the control of the
format. These strings are explained in the
DESCRIPTION
section.
The
printf
command converts, formats, and writes its arguments to the
standard output. The values specified by the
argument
variable are formatted under control of the
format
variable.
The argument variable is a list of one or more strings to be written to the standard output under the control of the format variable. These are treated as strings if the corresponding conversion character is b, c, or s; otherwise, it is evaluated as a C constant, with the following extensions:
A leading + (plus sign) or - (minus sign) is allowed.
If the leading character is a
'
(single quote) or
"
(double quote), the value is the numeric value in the underlying
code set of the character following the single quote or double quote.
The format variable is a character string that contains three types of objects: Plain characters that are copied to the output stream. The following escape sequences are both copied to the output stream and cause the associated action to occur on display devices that are capable of the action. Backslash Alert Backspace Formfeed Newline Carriage Return Tab Vertical Tab Where ddd is a one-, two-, or three-digit octal number. These escape sequences are displayed as a byte with the numeric value specified by the octal number. A delta character in the format string is treated as a delta character, rather than as a space character.
The format variable is reused as often as necessary to satisfy the arguments. Any extra c or s conversion specifications are evaluated as if a null string argument were supplied; other extra conversion specifications are evaluated as if a zero argument were supplied.
Each conversion specification in the format variable has the following syntax: A % (percent sign). Zero or more options, which modify the meaning of the conversion specification. The option characters and their meanings are as follows: The result of the conversion is left aligned within the field. The result of a signed conversion always begins with a + (plus) or - (minus). If the first character of a signed conversion is not a sign, a blank is prefixed to the result. If both the blank and + options appear, then the blank option is ignored. The value is converted to an alternative form. For c, d, i, u, and s conversions, the option has no effect. For o conversion, it increases the precision to force the first digit of the result to be a 0 (zero). For x and X conversions, a nonzero result has 0x, or 0X prefixed to it, respectively. For e, E, f, g, and G conversions, the result always contains a radix character, even if no digits follow the radix character. For g and G conversions, trailing zeros are not removed from the result as they usually are. For d, i, o, u, x, X, e, E, f, g, and G conversions, leading zeros (following any indication of sign or base) are used to pad to the field width, no space padding is performed. If the 0 (zero) and - options appear, the 0 (zero) option is ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 (zero) option is ignored. An optional decimal digit string that specifies the minimum field width. If the converted value has fewer characters than the field width, the field is padded on the left to the length specified by the field width. If the left-adjustment option flag (-) is specified, the field is padded on the right. An optional precision. The precision is a . (dot) followed by an asterisk (*), or a decimal digit string. If no precision is given, it is treated as 0 (zero). The precision specifies: The minimum number of digits to appear for the d, o, i, u, x, or X conversions. The number of digits to appear after the radix character for the e and f conversions. The maximum number of significant digits for the g conversion. The maximum number of bytes to be printed from a string in the s conversion. A character that indicates the type of conversion to be applied, as follows: Performs no conversion. Prints a % (percent sign). Accepts a value as a string that may contain backslash-escape sequences. Bytes from the converted string are printed until the end of the string or number of bytes indicated by the precision specification is reached. If the precision is omitted, all bytes until the first null character are printed.
If the result of a conversion is wider than the field width, the field
is expanded to contain the converted result. No truncation occurs. However,
a small precision may cause truncation on the right.
The following printf command formats a series of numbers: printf "%5d%4d\n" 1 21 321 4321 54321
The following environment variables affect the execution of
printf:
Provides a default value for the internationalization variables
that are unset or null. If
LANG
is unset or null, the corresponding value from the default locale is used.
If any of the internationalization variables contain an invalid setting, the
utility behaves as if none of the variables had been defined.
If set to a non-empty string value, overrides the values of
all the other internationalization variables.
Determines the locale for the interpretation of sequences
of bytes of text data as characters (for example, single-byte as opposed to
multibyte characters in arguments).
Determines the locale for the format and contents of diagnostic
messages written to standard error.
Determines the location of message catalogues for the processing
of
LC_MESSAGES.
Determines the locale for numbers written using the
e,
E,
f,
g,
or
G
conversion characters.
Commands: awk(1), bc(1), echo(1), read(1)
Functions: printf(3)
Standards: standards(5)