printf — Write formatted output
printf format [argument ...]
Purpose
printf writes the argument operands to standard output, formatted according to the format operand.
format is a format string 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
spaceand-appear as flags, thespaceflag is ignored. #- Prefixes octal values with
0and hexadecimal values with0xor0X. 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
0and-appear as flags, the0flag 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" 200The conversion character char is one of the following:
d- Decimal integer.
i- Decimal integer.
o- Unsigned octal integer.
x,X- Unsigned hexadecimal integer.
u- Unsigned decimal integer.
f,F- Floating point.
e,E- Floating point (scientific notation).
g,G- The shorter of e and f (suppresses insignificant zeros).
c- Single character of an integer value; the first character of a string.
s- String.
b- A string that may contain a backslash-escape sequence. Valid escape sequences are those
described in echo — Write arguments to standard output.
\0ddd- Where ddd is 0-to-3-digit octal number
\xdd- Where dd is a 0-to-2-digit hexadecimal number
\c- Indicates the first character of a string; number arguments are treated as strings.
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).
Localization
printf uses the following localization environment variables:
- LANG
- LC_ALL
- LC_CTYPE
- LC_MESSAGES
- LC_NUMERIC
Exit Values
Possible exit status values are:
0- Successful completion
>0- The number of failures due to any of the following:
- Missing format specifications
- Arguments 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, UNIX system V.
The %F format and the handling of * as a width or precision argument are
extensions of the POSIX standard.
Related Commands
echo, print