printf(1) UNIX System V(Essential Utilities) printf(1)
NAME
printf - print formatted output
SYNOPSIS
printf format [arg . . .]
DESCRIPTION
The printf command converts, formats, and prints its args under control
of the format. It fully supports conversion specifications for strings
(%s descriptor); however, the results are undefined for the other
conversion specifications supported by printf(3S).
format a character string that contains three types of objects: 1)
plain characters, which are simply copied to the output stream;
2) conversion specifications, each of which results in fetching
zero or more args; and 3) C-language escape sequences, which
are translated into the corresponding characters.
arg string(s) to be printed under the control of format. The
results are undefined if there are insufficient args for the
format. If the format is exhausted while args remain, the
excess args are simply ignored.
Each conversion specification is introduced by the character %. After
the %, the following appear in sequence:
An optional field, consisting of a decimal digit string followed by
a $, specifying the next arg to be converted. If this field is not
provided, the arg following the last arg converted is used.
An optional 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 given) to the field width. The padding is with blanks
unless the field width digit string starts with a zero, in which
case the padding is with zeros.
An optional precision that gives the maximum number of characters
to be printed from a string in %s conversion. The precision takes
the form of a period (.) followed by a decimal digit string; a
null digit string is treated as zero (nothing is printed). Padding
specified by the precision overrides the padding specified by the
field width. That is, if precision is specified, its value is used
to control the number of characters printed.
A field width or precision or both may be indicated by an asterisk
(*) instead of a digit string. In this case, an integer arg
supplies the field width or precision. The arg that is actually
converted is not fetched until the conversion letter is seen, so
the args specifying field width or precision must appear before the
arg (if any) to be converted. A negative field width argument is
taken as a `-' (left-adjustment) flag followed by a positive field
10/89 Page 1
printf(1) UNIX System V(Essential Utilities) printf(1)
width. If the precision argument is negative, it is changed to
zero (nothing is printed). In no case does a non-existent or small
field width cause truncation of a field; if the result of a
conversion is wider than the field width, the field is simply
expanded to contain the conversion result.
The conversion characters and their meanings are:
%s The arg is taken to be a string and characters from the string are
printed until a null character (\0) is encountered or the number of
characters indicated by the precision specification is reached. If
the precision is missing, it is taken to be infinite, so all
characters up to the first null character are printed. A null
value for arg yields undefined results.
%% Print a %; no argument is converted.
EXAMPLES
The command
printf '%s %s %s\n' Good Morning World
results in the output:
Good Morning World
The following command produces the same output.
printf '%2$s %s %1$s\n' World Good Morning
Here is an example that prints the first 6 characters of $PATH left-
adjusted in a 10-character field:
printf 'First 6 chars of %s are %-10.6s.\n' $PATH $PATH
If $PATH has the value /usr/bin:/usr/local/bin, then the above command
would print the following output:
First 6 chars of /usr/bin:/usr/local/bin are /usr/b .
SEE ALSO
printf(3S) in the Programmer's Reference Manual
Page 2 10/89