printf Command

Purpose

Writes formatted output.

Syntax

printf FormatArgument ... ]

Description

The printf command converts, formats, and writes its Argument parameters to standard output. The Argument parameters are formatted under control of the Format parameter. The formatted output line cannot exceed LINE_MAX bytes in length.

The following environment variables affect the execution of the printf command:

Item Description
LANG Determines the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_) do not specify a locale.
LC_ALL Determines the locale to be used to override any values for locale categories specified by the setting of LANG or any other LC_ environment variable.
LC_CTYPE Determines the locale for the interpretation of sequences of bytes of text data as characters; for example, single versus multibyte characters in parameters.
LC_MESSAGES Determines the language in which messages should be written.
LC_NUMERIC Determines the locale for numeric formatting. This environment variable affects the format of numbers written using the e, E, f, g, and G conversion characters.
The Format parameter is a character string that contains three types of objects:
  • Plain characters copied to the output stream.
  • Conversion specifications, each of which cause 0 or more items to be retrieved from the value parameter list.
  • The following escape sequences. When copied to the output stream, these sequences cause their associated action to be displayed on devices capable of the action:
    Item Description
    \\ Backslash
    \a Alert
    \b Backspace
    \f Form feed
    \n New line
    \r Carriage return
    \t Tab
    \v Vertical tab
    \ddd 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.

The Argument parameter is a list of one or more strings to be written to standard output under the control of the Format parameter.

The Format parameter is reused as often as necessary to satisfy the Argument parameters. 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 0 Argument were supplied. Where the Format parameter contains no conversion specifications and Argument parameters are present, the results are unspecified.

Each conversion specification in the Format parameter has the following syntax in this order:
  1. A % (percent sign).
  2. Zero or more options, which modify the meaning of the conversion specification. The option characters and their meanings are:
    Item Description
    - The result of the conversion is left-aligned within the field.
    + The result of a signed conversion always begins with a sign (+ or -).
    blank If the first character of a signed conversion is not a sign, a blank is prefixed to the result. If both the blank and + option characters are displayed, then the blank option character is ignored.
    # This option specifies that the value is to be converted to an alternate 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.
    0 For d, i, o, u, x, e, E, f, g, and G conversions, leading zeroes (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 the - (minus sign) options are displayed, 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.
    Note: For other conversions, the behavior is undefined.
  3. 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 is specified, the field is padded on the right. 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.
  4. An optional precision. The precision is a . (dot) followed by a decimal digit string. If no precision is given, it is treated as 0 (zero). The precision specifies:
    • The minimum number of digits to be displayed for the d, o, i, u, x, or X conversions.
    • The number of digits to be displayed 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.
  5. A character that indicates the type of conversion to be applied, such as:
    Item Description
    % Performs no conversion. Prints a % (percent sign).
    d, i Accepts an integer value and converts it to signed decimal notation. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros.
    o Accepts an integer value and converts it to signed octal notation. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros. An octal value for field width is not implied.
    u Accepts an integer value and converts it to unsigned decimal notation. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros.
    x, X Accepts an integer value and converts it to hexadecimal notation. The letters abcdef are used for the x conversion and the letters ABCDEF are used for the X conversion. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros.
    f Accepts a float or double value and converts it to decimal notation in the format [-] ddd.ddd. The number of digits after the radix character (shown here as the decimal point) is equal to the precision specification. The LC_NUMERIC locale category determines the radix character to use tin this format. If no precision is specified, then six digits are output. If the precision is 0 (zero), then no radix character will be displayed.
    e, E Accepts a float or double value and converts it to the exponential form [-] d.dde{+|-}dd. There is one digit before the radix character (shown here as the decimal point) and the number of digits after the radix character is equal to the precision specification. The LC_NUMERIC locale category determines the radix character to use tin this format. If no precision is specified, then six digits are output. If the precision is 0 (zero), then no radix character will be displayed. The E conversion character produces a number with E instead of e before the exponent. The exponent always contains at least two digits. However, if the value to be printed requires an exponent greater than two digits, additional exponent digits are printed as necessary.
    g, G Accepts a float or double value and converts it in the style of the f or e conversion characters (or E in the case of the G conversion), with the precision specifying the number of significant digits. Trailing zeros are removed from the result. A radix character is displayed only if it is followed by a digit. The style used depends on the value converted. Style g results only if the exponent resulting from the conversion is less than -4, or if it is greater than or equal to the precision.
    c Accepts a value as a string and prints the first character in the string.
    s Accepts a value as a string and prints characters from the string until the end of the string is encountered or the number of characters indicated by the precision is reached. If no precision is specified, all characters up to the first null character are printed.
    b 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.

The following backslash-escape sequences are supported:

  • The escape sequences previously listed above under the description of the Format parameter. These are converted to the individual characters they represented.
  • The \c (backslash c) sequence, which is not displayed and causes the printf command to ignore any remaining characters in the string parameter containing it, any remaining string parameters, and any additional characters in the Format parameter.

Exit Status

This command returns the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. Enter the following command:
    printf "%5d%4d\n" 1 21 321 4321 54321
    This produces the following output:
        1  21
      3214321
    54321   0
    The Format parameter is used three times to print all of the given strings. The 0 (zero) is supplied by the printf command to satisfy the last %4d conversion specification.
  2. Enter the following command:
    printf "%c %c\n" 78 79
    This produces the following output:
    7 7
  3. The following example demonstrates how the %$ format specifier can be used to print the date in an order different from the order of the arguments:
    printf (""%1$s, %3$d. %2$s, %4$d:%5$.2d", weekday, month, day, hour, min);
    Sunday, 3. July, 10:02
    (weekday, day. month, hour:min)
    

Files

Item Description
/usr/bin/printf Contains the printf command.