PRINTF(1P) | POSIX Programmer's Manual | PRINTF(1P) |
This 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...]
The printf utility shall write formatted operands to the standard output. The argument operands shall be formatted under control of the format operand.
None.
The following operands shall be supported:
Not used.
None.
The following environment variables shall affect the execution of printf:
Default.
See the EXTENDED DESCRIPTION section.
The standard error shall be used only for diagnostic messages.
None.
The format operand shall be used as the format string described in the Base Definitions volume of POSIX.1‐2017, Chapter 5, File Format Notation with the following exceptions:
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.
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:
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.
It shall not be considered an error if an argument operand is not completely used for a b, c, or s conversion.
The following exit values shall be returned:
Default.
The following sections are informative.
The 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‐2017 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‐2017 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.
To 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
The command:
printf "%5d%4d\n" 1 21 321 4321 54321
produces:
1 21
3214321 54321 0
Note that the format operand is used three times to print all of the given strings and that a '0' was supplied by printf to satisfy the last %4d conversion specification.
The printf utility is required to notify the user when
conversion errors are detected while producing numeric output; thus, the
following results would be expected on an implementation with 32-bit
twos-complement integers when %d is specified as the format
operand:
Standard | ||
Argument | Output | Diagnostic Output |
5a | 5 | printf: "5a" not completely converted |
9999999999 | 2147483647 | printf: "9999999999" arithmetic overflow |
-9999999999 | -2147483648 | printf: "-9999999999" arithmetic overflow |
ABC | 0 | printf: "ABC" expected numeric value |
The diagnostic message format is not specified, but these examples convey the type of information that should be reported. Note that the value shown on standard output is what would be expected as the return value from the strtol() function as defined in the System Interfaces volume of POSIX.1‐2017. A similar correspondence exists between %u and strtoul() and %e, %f, and %g (if the implementation supports floating-point conversions) and strtod().
In a locale using the ISO/IEC 646:1991 standard as the underlying codeset, the command:
printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"
produces:
Note that in a locale with multi-byte characters, the value of a character is intended to be the value of the equivalent of the wchar_t representation of the character as described in the System Interfaces volume of POSIX.1‐2017.
The printf utility was added to provide functionality that has historically been provided by echo. However, due to irreconcilable differences in the various versions of echo extant, the version has few special features, leaving those to this new printf utility, which is based on one in the Ninth Edition system.
The EXTENDED DESCRIPTION section almost exactly matches the printf() function in the ISO C standard, although it is described in terms of the file format notation in the Base Definitions volume of POSIX.1‐2017, Chapter 5, File Format Notation.
Earlier versions of this standard specified that arguments for all conversions other than b, c, and s were evaluated in the same way (as C constants, but with stated exceptions). For implementations supporting the floating-point conversions it was not clear whether integer conversions need only accept integer constants and floating-point conversions need only accept floating-point constants, or whether both types of conversions should accept both types of constants. Also by not distinguishing between them, the requirement relating to a leading single-quote or double-quote applied to floating-point conversions even though this provided no useful functionality to applications that was not already available through the integer conversions. The current standard clarifies the situation by specifying that the arguments for floating-point conversions are evaluated as if by strtod(), and the arguments for integer conversions are evaluated as C integer constants, with the special treatment of leading single-quote and double-quote applying only to integer conversions.
None.
awk, bc, echo
The Base Definitions volume of POSIX.1‐2017, Chapter 5, File Format Notation, Chapter 8, Environment Variables
The System Interfaces volume of POSIX.1‐2017, fprintf(), strtod()
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
2017 | IEEE/The Open Group |