printf (1p) - Linux Man Pages
printf: write formatted output
PROLOGThis manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
printf --- write formatted output
printf format [argument...]
DESCRIPTIONThe printf utility shall write formatted operands to the standard output. The argument operands shall be formatted under control of the format operand.
OPERANDSThe following operands shall be supported:
- A string describing the format to use to write the remaining operands. See the EXTENDED DESCRIPTION section.
- The strings to be written to standard output, under the control of format. See the EXTENDED DESCRIPTION section.
ENVIRONMENT VARIABLESThe following environment variables shall affect the execution of printf:
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2008, Section 8.2, Internationalization Variables the precedence of internationalization variables used to determine the values of locale categories.)
- If set to a non-empty string value, override the values of all the other internationalization variables.
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments).
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
Determine the locale for numeric formatting. It shall affect the format of numbers written using the e, E, f, g, and G conversion specifier characters (if supported).
- Determine the location of message catalogs for the processing of LC_MESSAGES.
STDOUTSee the EXTENDED DESCRIPTION section.
STDERRThe standard error shall be used only for diagnostic messages.
EXTENDED DESCRIPTIONThe format operand shall be used as the format string described in the Base Definitions volume of POSIX.1-2008, Chapter 5, File Format Notation with the following exceptions:
- A <space> in the format string, in any context other than a flag of a conversion specification, shall be treated as an ordinary character that is copied to the output.
- A '' character in the format string shall be treated as a '' character, not as a <space>.
- In addition to the escape sequences shown in the Base Definitions volume of POSIX.1-2008, Chapter 5, File Format Notation ('\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v'), "\ddd", where ddd is a one, two, or three-digit octal number, shall be written as a byte with the numeric value specified by the octal number.
- The implementation shall not precede or follow output from the d or u conversion specifiers with <blank> characters not specified by the format operand.
- The implementation shall not precede output from the o conversion specifier with zeros not specified by the format operand.
- The a, A, e, E, f, F, g, and G conversion specifiers need not be supported.
An additional conversion specifier character,
shall be supported as follows. The argument shall be taken to be a
string that may contain
sequences. The following
sequences shall be supported:
- The escape sequences listed in the Base Definitions volume of POSIX.1-2008, Chapter 5, File Format Notation ('\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v'), which shall be converted to the characters they represent
- "\0ddd", where ddd is a zero, one, two, or three-digit octal number that shall be converted to a byte with the numeric value specified by the octal number
- '\c', which shall not be written and shall cause printf to ignore any remaining characters in the string operand containing it, any remaining string operands, and any additional characters in the format operand
The interpretation of a <backslash> followed by any other sequence of characters is unspecified.
Bytes from the converted string shall be written until the end of the string or the number of bytes indicated by the precision specification is reached. If the precision is omitted, it shall be taken to be infinite, so all bytes up to the end of the converted string shall be written.
- For each conversion specification that consumes an argument, the next argument operand shall be evaluated and converted to the appropriate type for the conversion as specified below.
- The format operand shall be reused as often as necessary to satisfy the argument operands. Any extra c or s conversion specifiers shall be evaluated as if a null string argument were supplied; other extra conversion specifications shall be evaluated as if a zero argument were supplied. If the format operand contains no conversion specifications and argument operands are present, the results are unspecified.
- If a character sequence in the format operand begins with a '%' character, but does not form a valid conversion specification, the behavior is unspecified.
- The argument to the c conversion specifier can be a string containing zero or more bytes. If it contains one or more bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is unspecified whether nothing is written or a null byte is written.
The argument operands shall be treated as strings if the corresponding conversion specifier is b, c, or s, and shall be evaluated as if by the strtod() function if the corresponding conversion specifier is a, A, e, E, f, F, g, or G. Otherwise, they shall be evaluated as unsuffixed C integer constants, as described by the ISO C standard, with the following extensions:
- A leading <plus-sign> or minus-sign shall be allowed.
- If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.
- Suffixed integer constants may be allowed.
If an argument operand cannot be completely converted into an internal value appropriate to the corresponding conversion specification, a diagnostic message shall be written to standard error and the utility shall not exit with a zero exit status, but shall continue processing any remaining operands and shall write the value accumulated at the time the error was detected to standard output.
EXIT STATUSThe following exit values shall be returned:
- Successful completion.
- An error occurred.
CONSEQUENCES OF ERRORSDefault.
APPLICATION USAGEThe floating-point formatting conversion specifications of printf() are not required because all arithmetic in the shell is integer arithmetic. The awk utility performs floating-point calculations and provides its own printf function. The bc utility can perform arbitrary-precision floating-point arithmetic, but does not provide extensive formatting capabilities. (This printf utility cannot really be used to format bc output; it does not support arbitrary precision.) Implementations are encouraged to support the floating-point conversions as an extension.
Note that this printf utility, like the printf() function defined in the System Interfaces volume of POSIX.1-2008 on which it is based, makes no special provision for dealing with multi-byte characters when using the %c conversion specification or when a precision is specified in a %b or %s conversion specification. Applications should be extremely cautious using either of these features when there are multi-byte characters in the character set.
No provision is made in this volume of POSIX.1-2008 which allows field widths and precisions to be specified as '*' since the '*' can be replaced directly in the format operand using shell variable substitution. Implementations can also provide this feature as an extension if they so choose.
Hexadecimal character constants as defined in the ISO C standard are not recognized in the format operand because there is no consistent way to detect the end of the constant. Octal character constants are limited to, at most, three octal digits, but hexadecimal character constants are only terminated by a non-hex-digit character. In the ISO C standard, the "##" concatenation operator can be used to terminate a constant and follow it with a hexadecimal character to be written. In the shell, concatenation occurs before the printf utility has a chance to parse the end of the hexadecimal constant.
The %b conversion specification is not part of the ISO C standard; it has been added here as a portable way to process <backslash>-escapes expanded in string operands as provided by the echo utility. See also the APPLICATION USAGE section of echo for ways to use printf as a replacement for all of the traditional versions of the echo utility.
If an argument cannot be parsed correctly for the corresponding conversion specification, the printf utility is required to report an error. Thus, overflow and extraneous characters at the end of an argument being used for a numeric conversion shall be reported as errors.
EXAMPLESTo alert the user and then print and read a series of prompts:
printf "\aPlease fill in the following: \nName: " read name printf "Phone number: " read phone
To read out a list of right and wrong answers from a file, calculate the percentage correctly, and print them out. The numbers are right-justified and separated by a single <tab>. The percentage is written to one decimal place of accuracy:
while read right wrong ; do percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc) printf "%2d right\t%2d wrong\t(%s%%)\n" \ $right $wrong $percent done < database_file