printfdisplay a formatted string |
Command |
printf
format [argument ...]
printf
writes the argument operand(s) 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. The conversions are similar to those used by
the printf()
function in the ANSI C standard. Conversion
specifications have the form
where flag is one of the following:%[flag][width][.precision][char]
-
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 forces
the decimal point 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.
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 'br number, the precision is the number of digits to be printed
to the right of the decimal point in a floating point value. width or
precision may be specified as *
, in which case the value is
read from the next argument, which must be an integer. For example:
is equivalent toprintf "%*.*d\n" 20 10 200
The conversion character char is one of the following:printf "%20.10d\n" 200
b
a string which may contain a backslash-escape sequence. Valid escape
sequences are those described on the
echo
reference page,
\0
ddd where ddd is a zero- to three-digit octal
number, \x
dd where dd is a zero- to two digit
hexadecimal number, and \c
which causes
printf
to ignore the remainder of that argument,
any other arguments and the remainder of the format string.
c
the first character of a string; number arguments are
treated as strings. d
decimal integer. E,e
floating point (scientific notation). f
floating point.
G,g
the shorter of e
and f
(suppresses non-significant zeros). i
decimal integer.
o
unsigned octal integer. s
string.
u
unsigned decimal integer. X,x
unsigned
hexadecimal integer.printf
fills the remaining positions with null-strings
(character fields) or zeros (numeric fields).
0
Successful completion.
>0
The number of failures due to any of the following:
%
)
*
as a width or precision argument is an extension
to the POSIX standard. The \x
nn backslash escape for
hexadecimal is an extension to the POSIX standard.
printf
facility (like the C language
printf()
on which it is based) does not gracefully accommodate
multibyte characters when using %c
conversion, or with
either %b
or %s
conversions with a
specified precision. Use these features cautiously when you have multibyte
characters in the character set.