Contained WithinFind More DocumentationFeatured Support Resources | Download this book in PDF (7229 KB)
printf(1)Name | Synopsis | Description | Operands | Usage | Examples | Environment Variables | Exit Status | Attributes | See Also | Notes Name
Synopsisprintf format [argument]... ksh93printf format [string...] Description/usr/bin/printfThe printf utility writes each string operand to standard output using format to control the output format. Operands
/usr/bin/printfThe following operands are supported by /usr/bin/printf: ksh93The format operands support the full range of ANSI C/C99/XPG6 formatting specifiers as well as additional specifiers: When performing conversions of string to satisfy a numeric format specifier, if the first character of string is "or', the value is the numeric value in the underlying code set of the character following the "or'. Otherwise, string is treated like a shell arithmetic expression and evaluated. If a string operand cannot be completely converted into a value appropriate for that format specifier, an error occurs, but remaining string operands continue to be processed. In addition to the format specifier extensions, the following extensions of ANSI C/C99/XPG6 are permitted in format specifiers: If there are more string operands than format specifiers, the format string is reprocessed from the beginning. If there are fewer string operands than format specifiers, then string specifiers are treated as if empty strings were supplied, numeric conversions are treated as if 0 was supplied, and time conversions are treated as if now was supplied. /usr/bin/printf is equivalent to ksh93's printf built-in and print -f, which allows additional options to be specified. Usage/usr/bin/printfThe printf utility, like the printf(3C) function on which it is based, makes no special provision for dealing with multi-byte characters when using the %c conversion specification. Applications should be extremely cautious using either of these features when there are multi-byte characters in the character set. Field widths and precisions cannot be specified as *. 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 USAGE section of the echo(1) manual page 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 reports an error. Thus, overflow and extraneous characters at the end of an argument being used for a numeric conversion are to be reported as errors. It is not considered an error if an argument operand is not completely used for a c or s conversion or if a string operand's first or second character is used to get the numeric value of a character. Examples/usr/bin/printfExample 1 Printing a Series of PromptsThe following example alerts the user, then prints and reads a series of prompts:
Example 2 Printing a Table of CalculationsThe following example prints a table of calculations. It reads out a list of right and wrong answers from a file, calculates the percentage correctly, and prints them out. The numbers are right-justified and separated by a single tab character. The percentage is written to one decimal place of accuracy:
Example 3 Printing number stringsThe command:
produces:
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. Example 4 Tabulating Conversion ErrorsThe following example tabulates conversion errors. The printf utility tells the user when conversion errors are detected while producing numeric output. These results would be expected on an implementation with 32-bit twos-complement integers when %d is specified as the format operand:
The value shown on standard output is what would be expected as the return value from the function strtol(3C). A similar correspondence exists between %u and strtoul(3C), and %e, %f and %g and strtod(3C). Example 5 Printing Output for a Specific LocaleThe following example prints output for a specific locale. In a locale using the ISO/IEC 646:1991 standard as the underlying codeset, the command:
produces:
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. If an argument operand cannot be completely converted into an internal value appropriate to the corresponding conversion specification, a diagnostic message is written to standard error and the utility does exit with a zero exit status, but continues processing any remaining operands and writes the value accumulated at the time the error was detected to standard output. Example 6 Alternative floating point representation 1The printf utility supports an alternative floating point representation (see printf(3C) entry for the "%a"/"%A"), which allows the output of floating-point values in a format that avoids the usual base16 to base10 rounding errors.
produces:
Example 7 Alternative floating point representation 2The following example shows two different representations of the same floating-point value.
produces:
Example 8 Output of unicode valuesThe following command will print the EURO unicode symbol (code-point 0x20ac).
produces:
where "<euro>" represents the EURO currency symbol character. Example 9 Convert unicode character to unicode code-point valueThe following command will print the hexadecimal value of a given character.
where "<euro>" represents the EURO currency symbol character (code-point 0x20ac). produces:
Example 10 Print the numeric value of an ASCII character
produces:
Example 11 Print the language-independent date and time formatTo print the language-independent date and time format, the following statement could be used:
For example,
For American usage, format could be the string:
producing the message:
Whereas for EU usage, format could be the string:
Note that the '$' characters must be properly escaped, such as
producing the message:
Environment VariablesSee environ(5) for descriptions of the following environment variables that affect the execution of printf: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, LC_NUMERIC, and NLSPATH. Exit StatusAttributesSee attributes(5) for descriptions of the following attributes: /usr/bin/printf
ksh93See Alsoawk(1), bc(1), date(1), echo(1), ksh93(1), printf(3C), strtod(3C), strtol(3C), strtoul(3C), attributes(5), environ(5), formats(5), standards(5) NotesUsing format specifiers (characters following '%') which are not listed in the printf(3C) or this manual page will result in undefined behavior. Using escape sequences (the character following a backslash ('\')) which are not listed in the printf(3C) or this manual page will result in undefined behavior. Floating-point values follow C99, XPG6 and IEEE 754 standard behavior and can handle values the same way as the platform's |long double| datatype. Floating-point values handle the sign separately which allows signs for values like NaN (for example, -nan), Infinite (for example, -inf) and zero (for example, -0.0). Name | Synopsis | Description | Operands | Usage | Examples | Environment Variables | Exit Status | Attributes | See Also | Notes |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||