printf(1) printf(1)
NAME
printf - formatted output
SYNOPSIS
printf 'format' [arg ...]
DESCRIPTION
The printf command outputs the arguments you specify in formatted
form. printf supports most of the format specifications for strings as
in the printf(3S) function in C.
OPERANDS
'format'
Character string that can contain three different types of
objects:
- plain characters, which are output without any modifications.
- C-language escape sequences, which are converted into the cor-
responding characters in the output, e.g. \n is converted to a
newline character.
- Format elements from which each one of the specified arguments
arg is processed.
Metacharacters
The following metacharacters are interpreted by printf:
\\ Backslash (for distinguishing octal characters)
\a Warning, alert
\b Backspace
\f Form feed
\n Newline
\r Carriage Return
\t Tab
\v Vertical tab
\octal Octal number, whereby octal consists of one, two, or
three digits
Format elements
A conversion specification comprises:
Page 1 Reliant UNIX 5.44 Printed 11/98
printf(1) printf(1)
%[argno$][fieldwidth][.precision]conversioncharacter
% Always located at the beginning of the conversion specifica-
tion. If the % character is not to be interpreted as a part
of the conversion specification but as an ordinary character
to be output, it must be escaped by preceding it with
another % (%%).
argno$
Decimal integer with which you specify the position of the
argument to be processed. The number must be followed by a $
character.
argno not specified: The argument following the last con-
verted argument is used.
Note: If you use argno$ for one argument, you should also
use argno$ for all the other arguments.
fieldwidth
Decimal integer with which you specify the minimum field
width. If the string or number to be converted has fewer
characters/digits than fieldwidth, it is padded on the left
to the field width and output right-adjusted. If left-adjust-
ment is desired, the decimal integer must be preceded by a
dash (-). The padding is with blanks unless the fieldwidth
integer starts with a zero, in which case padding for right-
adjusted output is done with zeros.
A fieldwidth may also be indicated by an asterisk (*)
instead of an integer. In this case, an integer argument
supplies the field width. This argument must appear before
the string or number to be converted. The asterisk does not
work in combination with argno$.
If the string or number is longer than the field width, the
field is automatically expanded.
.precision
Decimal integer with which you specify the maximum number of
characters to be output from the string or number to be con-
verted. This number must be preceded by a dot (.). If the
precision argument is zero, nothing is output. The number of
characters output is always controlled by the precision,
even if some other value has been specified for the field
width.
A precision may also be indicated by an asterisk (*) instead
of an integer. In this case, an integer argument supplies
the precision. This argument must appear before the string
or number to be converted. The asterisk does not work in
combination with argno$.
Page 2 Reliant UNIX 5.44 Printed 11/98
printf(1) printf(1)
conversioncharacter
The following conversioncharacters can be used for printf:
____________________________________________________________
| Conversion| |
| character | Meaning |
|___________|_______________________________________________|
| b | character string with metacharacters |
|___________|_______________________________________________|
| c | single character |
|___________|_______________________________________________|
| d, i | signed decimal integer |
|___________|_______________________________________________|
| e, E | floating point number in exponential nota- |
| | tion, e.g. 5.234e+2 |
|___________|_______________________________________________|
| f | floating point number, e.g. 52.34 |
|___________|_______________________________________________|
| g, G | %e or %f, whichever is shorter |
|___________|_______________________________________________|
| o | signed octal integer (base 8) |
|___________|_______________________________________________|
| s | character string |
|___________|_______________________________________________|
| u | unsigned decimal integer |
|___________|_______________________________________________|
| x, X, p | unsigned hexadecimal integer (base 16) |
|___________|_______________________________________________|
With s, all characters from the character string are output
until the number of characters specified for precision is
reached. If precision is not specified, the entire character
string is output
With b, the character string in arg can contain metacharac-
ters. printf supports all escape sequences interpreted by
the echo command in this case, i.e. the escape sequences
specified above with the exception of octal numbers speci-
fied as \0octal, and \c. \c causes printf to abort output at
this point and not to terminate it with a newline character.
arg String to be written to standard output in the format specified
by format. If there are less arguments available than are
required by format, 0 or the null string are used for the missing
arguments. If more arguments are available than are required by
format, format is used several times (except where argno$ is
specified; the excess arguments are ignored in this case).
arg not specified:
The result is undefined.
Page 3 Reliant UNIX 5.44 Printed 11/98
printf(1) printf(1)
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which
affect internationalization.
EXAMPLES
Example 1
Output the string "Good Morning Helen" on the screen:
$ printf '%s %s %s\n' Good Morning Helen
The following command produces the same output:
$ printf '%2$s %3$s %1$s\n' Helen Good Morning
Example 2
Output the first 6 characters of your home directory /usr/kathy with
an appropriate message:
$ printf 'The first 6 characters of %s are %.6s.\n' $HOME $HOME
The first 6 characters of /usr/kathy are /usr/k.
Example 3
The examples that follow write a 4-digit number in an 8-character
field right-adjusted, right-adjusted with leading zeros, and left-
adjusted, respectively:
$ printf '%8s\n' 1860
1860
$ printf '%08s\n' 1860
00001860
$ printf '%-8s\n' 1860
1860<blank><blank><blank><blank>
Page 4 Reliant UNIX 5.44 Printed 11/98
printf(1) printf(1)
Example 4
Output a fixed point number limited to two decimal places:
$ printf '%.2f\n' 1000.3333333
1000.33
Note: Where the LANG variable is German, a decimal comma must be
entered instead of the decimal point.
SEE ALSO
awk(1), bc(1), echo(1), printf(3S).
Page 5 Reliant UNIX 5.44 Printed 11/98