Format
printf format [argument
…]
Description
printf writes
the argument operands to standard output,
formatted according to the format operand.
format is
a format string that is composed of conversion specifications that
convert and add the next
argument to the
output.
format can contain backslash-escape
sequences. These conversions are similar to those used by the ANSI
C standard. Conversion specifications have the form:
%[flag][width]
[precision][char]
where
flag is
one of the following:
- -
- Left-justifies the field; default is right justification.
- +
- Always prefixes a signed value with a sign (+ or -).
- space
- Reserves a character position at the start of the string for the
minus sign (for negative numbers) or a space (for positive numbers).
If both space and - appear as flags,
the space flag is ignored.
- #
- Prefixes octal values with 0 and hexadecimal
values with 0x or 0X. For floating-point
values, this causes the decimal point always to be displayed even
if no characters follow it.
- 0
- Pads numeric values with leading zeros. If both 0 and - appear
as flags, the 0 flag is ignored.
width is the minimum
field width of the output field. If the converted value is shorter
than the minimum width, printf pads it with
spaces or zeros.
In a string,
precision is
the maximum number of bytes to be printed from the string; in a number,
the precision is the number of digits to be printed to right of the
decimal point in a floating-point value.
width or
precision can
be specified as
*, in which case the value is read
from the next argument, which must be an integer. For example:
printf "%*.*d\n" 20 10 200
is equivalent to:
printf "%20.10d\n" 200
The
conversion character
char is one of the
following:
- b
- A string that may contain a backslash-escape sequence.
- c
- Single character of an integer value; the first character of a
string.
- d
- Decimal integer.
- e,E
- Floating point (scientific notation).
- f,F
- Floating point.
- g,G
- The shorter of e and f (suppresses nonsignificant zeros).
- i
- Decimal integer.
- o
- Unsigned octal integer.
- s
- String.
- u
- Unsigned decimal integer.
- x,X
- Unsigned hexadecimal integer.
When there are more arguments than positions
in format, the format string
is applied again to the remaining arguments. When there are fewer
arguments than there are positions in the format string, printf fills
the remaining positions with null strings (character fields) or zeros
(numeric fields).
Caution
The POSIX.2POSIX.2 printf facility
(like the C language printf() on which it
is based), does not accommodate double-byte characters gracefully
when using %c conversion, or either of %b or %s conversions
with a specified precision. Use these features cautiously when you
have double-byte characters in the character set.
In a double-byte
environment, normal backslash-escape characters are handled correctly
(printf shifts state as required) but octal
and hexadecimal escape characters do not change state. This behavior
is significant in a shift-lock environment. For example, if an octal
escape character contains the shift-in character, it is the user's
responsibility to ensure that there is also a shift-out character.
Further, an octal or hexadecimal backslash escape character that
comes immediately after a double-byte character may or may not be
processed in the shifted state.
For more information about
double-byte character environments, see Using the double-byte character set (DBCS).
Localization
printf uses
the following localization environment variables:
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_NUMERIC
- LC_SYNTAX
- NLSPATH
See Localization for more
information.
Exit values
- 0
- Successful completion
- >0
- The number of failures due to any of the following:
- Missing format specifications
- Arguments that were supplied for a format string
that does not accept them (that is, that has no %s)
- Incorrect integer argument
- Incorrect floating-point argument
Portability
POSIX.2, X/Open Portability Guide, UNIX System V.
The %F format
and the handling of * as a width or precision argument
are extensions of the POSIX standard.
Related information
echo, print