printf formats the given arguments according to
the format string, and writes them to the standard output.
format is a character string that can contain:
plain characters that are simply copied to the standard output
conversion specifications in the style of
printf(S-osr5),
each of which is used to format zero or more arguments
escape sequences that represent non-graphic characters
printf repeatedly applies format until all
arguments have been displayed. The output for a missing
argument will be 0 (zero) or a null string as appropriate.
A conversion specification has the form:
%[n$][flags][width][.precision] specifier
Between the initial ``%'' and the specifier, there
may be an argument positional specifier (n$), argument
display flags, and values specifying width and precision.
An argument positional specifier selects the nth
argument. The maximum value of n specifies the
number of arguments to be processed at a time. Further arguments are
processed by applying format to them alone. Argument
positional specifiers, if used, must be provided for all the
specifiers.
One or more of the following flags may appear:
-
Justify the result of the conversion to the left within the field
(the default is to justify to the right). This is meaningless unless
a width is specified.
+
Precede the result of a signed numeric conversion with a sign (+ or
) (the default is to output minus signs ``-'' only).
<Space>
Precede the result of a signed numeric conversion with a space or
minus sign ``-''. This flag is ignored if the plus sign flag
``+'' is given.
#
With the ``o'', ``x'', and ``X'' specifiers,
precede the converted numeric argument with ``0'',
``0x'', and ``0X'' respectively. With the ``e'',
``E'', ``f'', ``g'', and ``G'' specifiers,
the radix character (decimal point) is always present in the printed
value. Additionally, this flag displays any trailing zeros for the
``g'', and ``G'' keyletters (these are usually removed).
0
If the minus sign flag ``-'' is not present and no
precision is specified, pad numeric values with the zero
digit ``0'' instead of space.
width is a decimal digit string specifying a minimum field
width. If the converted value has fewer characters than the field
width, it is padded on the left (or right, if the left adjustment
flag "" has been specified). The padding is done with spaces
unless the first character of width is a zero, in which
case the padding is done with zeros.
precision is a decimal digit string succeeding a dot
``.'' specifying the minimum number of digits to appear for
the ``d'', ``o'', or ``x'' conversions, or the
maximum number of characters to be printed in the ``s''
conversion.
One of the following conversion specifiers must appear:
Keyletter
Prints argument as
b
a string that may contain the following backslash escape sequences:
escape sequences that are converted into the characters they
represent: ``\\'' (backslash), ``\a'' (alert),
``\b'' (backspace), ``\f'' (formfeed), ``\n''
(newline), ``\r'' (carriage return), ``\t''
(horizontal tab), ``\v'' (vertical tab)
``\c'', which is not written, and which causes
printf to ignore any remaining characters in
format
``\0nnn'', where nnn is a 1, 2 or 3-digit
octal number to be converted to a byte with the corresponding octal
value
c
the character referred to by the least significant 8
bits of the numeric value of argument; truncates
argument to the nearest integer
d
a decimal integer (0 if no arguments are available); truncates
argument to the nearest integer
e
a floating point number (0.0 if no arguments are available); the
format used is
``[-]d.ddde[+|-]dd'' where the
number of digits after the decimal point is controlled by the value
of precision
E
a floating point number (0.0 if no arguments are available); the
format used is
``[-]d.dddE[+|-]dd'' where the
number of digits after the decimal point is controlled by the value
of precision
f
a floating point number (0.0 if no arguments are available); the
format used is ``[-]ddd.ddd'' where the
number of digits after the decimal point is controlled by the value
of precision
g
a floating point number (0.0 if no arguments are available); the
format used is the most compact result from applying the ``e''
and ``f'' conversions
G
a floating point number (0.0 if no arguments are available); the
format used is the most compact result from applying the ``E''
and ``f'' conversions
i
a decimal integer (0 if no arguments are available); truncates
argument to the nearest integer
o
an unsigned octal number (0 if no arguments are available);
truncates argument to the nearest integer
s
a string
u
an unsigned decimal integer (0 if no arguments are available);
truncates argument to the nearest integer
x
an unsigned hexadecimal number displayed in lowercase display (0 if
no arguments are available); truncates argument to the
nearest integer
X
an unsigned hexadecimal number displayed in uppercase display (0 if
no arguments are available); truncates argument to the
nearest integer
%
prints a ``%''; no argument is converted
If the first character of an argument is a single quote
``''' or a double quote ``"'', the value to be
converted will be the numeric value of the next character in the
argument in the current codeset.
The following (backslash) escape sequences are recognized within
format strings:
Escape sequence
Meaning
\\
backslash
\a
alert or bell
\b
backspace
\c
ignore any remaining characters in the format string, and
any remaining arguments (use with ``b'' specifier
only)
\f
formfeed
\n
newline
\r
carriage return
\t
horizontal tab
\v
vertical tab
\0nnn
the character with value given by the one, two, or three digit octal
number nnn in the current codeset (use with ``b''
specifier only)
Examples
Alert and request some input from a user:
#!/bin/ksh
printf "\aPlease fill in the following\nName: "
read name
printf "Phone number: "
read phone
Display the user ID of each user in /etc/passwd:
#!/bin/ksh
IFS=":"
FORMAT="%s's UID is %d\n"
while true
do
read -r uname passwd uid remainder || exit
printf $FORMAT $uname $uid
done < /etc/passwd
printf "%-5d%4d" 1 \`22 333 produces the output:
1 50
333 0
In this example, the format specification is used twice
because there are three arguments for two conversion specifications
in the format string. printf prints a zero ``0'' for
the missing fourth argument.
The ``-'' flag in the first conversion specifier causes the
first and third arguments to be displayed left-justified.
The second argument is printed as the decimal string corresponding
to the ASCII value (``50'') of the character
``2'' because it was preceded by a single quote. The
second ``2'' is ignored. The quote is escaped with backslash
to prevent the shell trying to find a closing quote; alternatively,
one of the following strings could be used:
\"22"'22"
Unlike
printf(S-osr5),
the ``'' character cannot be used to stand for a
width or precision value. This functionality can
be simulated using the string concatenation of the shell, and
defining shell variables to hold the width and precision values:
An example of using the argument positional specifiers (note that
the ``$''s are protected from being expanded by the shell):
$ printf '[%2$d %1$s]\n' string 10
[10 string]
$ printf "[%3\$d %1\$s]\n" a b c d e f
[c a]
[f d]
Limitations
printf cannot format output from
bc(C)
since it does not support arbitrary precision.
printf makes no special provision for dealing with multibyte
characters when using the %c conversion specification, or
when a precision is specified in a %b or %s
conversion specification. Applications should be extremely cautious
using either of these features when there are multibyte characters
in the character set.