PRINTF(1) General Commands Manual PRINTF(1)

printfformat data

printf [--] format [data]…

Writes format to the standard output stream, interpreting escape sequences and data conversions.

If there are any % conversion specifiers, this is repeated until all data are exhausted.

, \b, \t, \n, \v, \f, \r, \e
The bell, backspace, tab, line feed, vertical tab, form feed, carriage return and escape characters, respectively.
, \\
Just " and \.
Abort all processing.
\O, \OO, \OOO
Byte corresponding to octal value O (OO, OOO).
X, \xXX
Byte corresponding to hexadecimal value X (XX).
XXXX, \UXXXXXXXX
A representation of the Unicode character corresponding to the hexadecimal value XXXX (XXXXXXXX) in the current locale. If the character is out of range (0 or greater than ) or the conversion failed, "\uXXXX" if ("\UXXXXXXXX" otherwise) is output instead.
All others
passed through.

For an in-depth description of conversion specifiers, see printf(3). The format used is identical, except %b and %q are added, and size specifiers (like ll, h, L for long long, short, long double) have no effect: all integers are 64-bit [unsigned] long longs, and all floating-point numbers are 128-bit long doubles.

If there are more conversion specifiers than data, 0 is used for numeric conversions (%diouxXeEfFgGaAcC) and the null string for string conversions (%sSbq).

Partial conversion yields a diagnostic, but processing otherwise continues. Numbers can also be specified as 'C or "C, in which case they're equal to the value of the character in the current locale following '" (the next byte if invalid, or 0 if none).

Variable width and precision (%*.*d) are supported, and those arguments are intepreted as ints.

A literal ; no data.

, %i
Signed decimal integer.
Unsigned decimal integer.
Unsigned octal integer.
, %X
Unsigned hexadecimal integer. With %X – upper-case letters.

, %E
Floating-point number in exponent (1.23456) format. With %E – capital , NAN, &c.
, %F
Floating-point number decimal (123.456) format rounded to precision (default: 6). With %F – capital NAN, , &c.
, %G
Equivalent to %f for floating-point numbers if ≥ 0.0001 and precision (default: 6), otherwise %e. With %G%E, %F.
, %A
Floating-point number in hexadecimal exponent (a.bcde±f) format. With %A – capital letters, , NAN, &c.

, %C
First byte of data.
, %S
data

data with \ escapes intepreted, but octal \O[O[O]] escapes may also be prefixed with a 0 (like \0O[O[O]]). If a precision is specified, limit output to that many bytes.

data in a format that can be used to fully recover it as a single token in an Almquist shell — printable characters are wrapped in ', others as octal escapes, except for \a, \b, \t, \n, \v, \f, and \r. If a precision is specified, format that many bytes.

1 if no digits were specified for an \x escape, not enough digits for \u and \U escapes or no conversion, an invalid, or an unknown one was specified after a %; these conditions also immediately abort processing. Additionally, 1 is returned but processing continues if a non-'" number had trailing data or parsing failed altogether,

Assuming a default UTF-8 locale:

$ printf '%02X;' 12 012 0x12 \"A  # no newline
0C;0A;12;41;
$ printf '%-7s: %gkg\t$%.2f\n' Bananas 3.5 4.51 Kiwis 2 3.19 Bread 20.21
Bananas: 3.5kg  $4.51
Kiwis  : 2kg    $3.19
Bread  : 20.21kg        $0.00

$ printf '\44\x9\U0001F629%0*d\n' 3 \"Q
$       😩081
$ LC_ALL=C printf '\44\x9\U0001F629%0*d\n' 3 \"Q
$       \U0001F629081

$ printf '%q\n' "$(printf '\44\x9\U0001F629%0*d' 3 \"Q)"
'$'$'\t''😩081'
$ LC_ALL=C printf '%q\n' "$(printf '\44\x9\U0001F629%0*d' 3 \"Q)"
'$'$'\t\360\237\230\251''081'

printf(3), strtold(3), strtoull(3)

Conforms to IEEE Std 1003.1-2008 (“POSIX.1”), except that floating-point numbers are parsed with strtold() rather than strtod() and are allowed a '" prefix . POSIX specifies only the following escapes in the format: \\, \a, \b, \f, \n, \r, \t, \v, and \O[O[O]]; and in %b: \\, \a, \b, \f, \n, \r, \t, \v, \0O[O[O]], and \c. The standard specifies floating-point (%aAeEfFgG) conversions as optional, and requires only %diouxXcs and %b.

%CS are extensions, provided for exhaustion of IEEE Std 1003.1-2008 (“POSIX.1”) against X/Open Systems Interfaces (XSI) printf(3); they're equivalent to %lc and %ls, and hence equivalent to %cs. Don't use them.

%q is an extension, originating from the GNU system, whose printf doesn't understand the precision argument to %b — this is a conformance bug, nor %q — this implementation's is an extension.

Beyond what's specified by the standard, most systems support a wild array of \ escapes and conversions; be wary.

Created by X/Open Portability Guide Issue 4 (“XPG4”) to provide a portable way to mimic AT&T System V Release 3 UNIX echo with %b, in contrast to the incompatible Version 7 AT&T UNIX echo, only supporting first-argument -n, cf. echo(1).

October 26, 2022 voreutils pre-v0.0.0-latest