printf

display a formatted string

Command


SYNOPSIS

printf format [argument ...]


DESCRIPTION

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
%[flag][width][.precision][char]
where flag is one of the following:
-

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.

width is the minimum field width of the output field. If the converted value is shorter than the minimum width, 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:
printf "%*.*d\n" 20 10 200
is equivalent to
printf "%20.10d\n" 200
The conversion character char is one of the following:
b

a string which may contain a backslash-escape sequence. Valid escape sequences are those described on the echo reference page, \0ddd where ddd is a zero- to three-digit octal number, \xdd 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.
When there are more arguments than positions in format, the format string is applied again to the remaining arguments. When there are fewer arguments than positions in the format string, printf fills the remaining positions with null-strings (character fields) or zeros (numeric fields).


DIAGNOSTICS

Possible exit status values are:
0

Successful completion.

>0

The number of failures due to any of the following:

— missing format specifications
arguments supplied for a format string which does not
accept them (that is, has no %)
— invalid integer argument
— invalid floating point argument


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0.

The handling of * as a width or precision argument is an extension to the POSIX standard. The \xnn backslash escape for hexadecimal is an extension to the POSIX standard.


WARNING

The POSIX.2 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.


SEE ALSO

Commands:
echo, print


Updated MKS Toolkit [3khat16.ico]HP3000 [3khat16.ico]3kMail [archive16.gif]