printf - Write formatted output
Format
printf format [argument ...]
Description
printf writes the argument operands to standard output, formatted according to the format operand.
%[flag][width]
[precision][char]
where flag is one of the following options: -
- 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, thespace
flag is ignored. #
- Prefixes octal values with
0
and hexadecimal values with0x
or0X
. 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, the0
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 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
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.
Localization
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_NUMERIC
- LC_SYNTAX
- NLSPATH
Exit values
0
- Successful completion.
>0
- The number of failures due to any of the following reasons:
- 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, printf