The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

printf - write formatted output

 SYNOPSIS



printf format[argument...]

 DESCRIPTION

The printf utility will write formatted operands to the standard output. The argument operands will be formatted under control of the format operand.

 OPTIONS

None.

 OPERANDS

The following operands are supported:
format
A string describing the format to use to write the remaining operands; see the EXTENDED DESCRIPTION section.
argument
The strings to be written to standard output, under the control of format; see the EXTENDED DESCRIPTION section.

 STDIN

Not used.

 INPUT FILES

None.

 ENVIRONMENT VARIABLES

The following environment variables affect the execution of printf:
LANG
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
LC_ALL
If set to a non-empty string value, override the values of all the other internationalisation variables.
LC_CTYPE
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).
LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
LC_NUMERIC
Determine the locale for numeric formatting. It will affect the format of numbers written using the e, E, f, g and G conversion characters (if supported).
NLSPATH
Determine the location of message catalogues for the processing of LC_MESSAGES .

 ASYNCHRONOUS EVENTS

Default.

 STDOUT

See the EXTENDED DESCRIPTION section.

 STDERR

Used only for diagnostic messages.

 OUTPUT FILES

None.

 EXTENDED DESCRIPTION

The format operand will be used as the format string described in the XBD specification, File Format Notation  with the following exceptions:

The argument operands will be treated as strings if the corresponding conversion character is b, c or s; otherwise, it will be evaluated as a C constant, as described by the ISO C standard, with the following extensions:

If an argument operand cannot be completely converted into an internal value appropriate to the corresponding conversion specification, a diagnostic message will be written to standard error and the utility will not exit with a zero exit status, but will continue processing any remaining operands and will write the value accumulated at the time the error was detected to standard output.

 EXIT STATUS

The following exit values are returned:
0
Successful completion.
>0
An error occurred.

 CONSEQUENCES OF ERRORS

Default.

 APPLICATION USAGE

The floating-point formatting conversion specifications of printf() are not required because all arithmetic in the shell is integer arithmetic. The awk utility performs floating-point calculations and provides its own printf function. The bc utility can perform arbitrary-precision floating-point arithmetic, but does not provide extensive formatting capabilities. (This printf utility cannot really be used to format bc output; it does not support arbitrary precision.) Implementations are encouraged to support the floating-point conversions as an extension.

Note that this printf utility, like the XSH specification printf() function on which it is based, makes no special provision for dealing with multi-byte 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 multi-byte characters in the character set.

Field widths and precisions cannot be specified as "*" since the "*" can be replaced directly in the format operand using shell variable substitution. Implementations can also provide this feature as an extension if they so choose.

Hexadecimal character constants as defined in the ISO C standard are not recognised in the format operand because there is no consistent way to detect the end of the constant. Octal character constants are limited to, at most, three octal digits, but hexadecimal character constants are only terminated by a non-hex-digit character. In the ISO C standard, the ## concatenation operator can be used to terminate a constant and follow it with a hexadecimal character to be written. In the shell, concatenation occurs before the printf utility has a chance to parse the end of the hexadecimal constant.

The %b conversion specification is not part of the ISO C standard; it has been added here as a portable way to process backslash escapes expanded in string operands as provided by the echo utility. See also the APPLICATION USAGE section of echo for ways to use printf as a replacement for all of the traditional versions of the echo utility.

If an argument cannot be parsed correctly for the corresponding conversion specification, the printf utility is required to report an error. Thus, overflow and extraneous characters at the end of an argument being used for a numeric conversion are to be reported as errors.

It is not considered an error if an argument operand is not completely used for a c or s conversion or if a string operand's first or second character is used to get the numeric value of a character.

 EXAMPLES

To alert the user and then print and read a series of prompts:

printf "\aPlease fill in the following: \nName: "
read name
printf "Phone number: "
read phone

To read out a list of right and wrong answers from a file, calculate the percentage correctly, and print them out. The numbers are right-justified and separated by a single tab character. The percentage is written to one decimal place of accuracy:


while read right wrong ; do
   percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
   printf "%2d right\t%2d wrong\t(%s%%)\n" \
       $right $wrong $percent
done < database_file

The command:

printf "%5d%4d\n" 1 21 321 4321 54321

produces:

    1  21
  3214321
54321   0

Note that the format operand is used three times to print all of the given strings and that a 0 was supplied by printf to satisfy the last %4d conversion specification.

The printf utility is required to notify the user when conversion errors are detected while producing numeric output; thus, the following results would be expected on an implementation with 32-bit twos-complement integers when %d is specified as the format operand:

Argument Standard
Output
Diagnostic Output
5a 5 printf: "5a" not completely converted
9999999999 2147483647 printf: "9999999999" arithmetic overflow
-9999999999 -2147483648 printf: "-9999999999" arithmetic overflow
ABC 0 printf: "ABC" expected numeric value

The diagnostic message format is not specified, but these examples convey the type of information that should be reported. Note that the value shown on standard output is what would be expected as the return value from the XSH specification function strtol(). A similar correspondence exists between %u and strtoul() and %e, %f and %g (if the implementation supports floating-point conversions) and strtod().

In a locale using the ISO/IEC 646:1991 standard as the underlying codeset, the command:


printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"

produces:
3 Numeric value of constant 3
3 Numeric value of constant 3
-3 Numeric value of constant -3
51 Numeric value of the character `3' in the ISO/IEC 646:1991 standard codeset
43 Numeric value of the character `+' in the ISO/IEC 646:1991 standard codeset
45 Numeric value of the character `-' in the ISO/IEC 646:1991 standard codeset

Note that in a locale with multi-byte characters, the value of a character is intended to be the value of the equivalent of the wchar_t representation of the character as described in the XSH specification.

 FUTURE DIRECTIONS

None.

 SEE ALSO

awk, bc, echo, the XSH specification description of printf().

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]