15.2 printf: Format and print data

printf does formatted printing of text. Synopsis:

printf format [argument]…

printf prints the format string, interpreting ‘%’ directives and ‘\’ escapes to format numeric and string arguments in a way that is mostly similar to the C ‘printf’ function. See printf format directives in The GNU C Library Reference Manual, for details. The differences are listed below.

Due to shell aliases and built-in printf functions, using an unadorned printf interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., env printf …) to avoid interference from the shell.

A floating point argument is interpreted according to the LC_NUMERIC category of either the current or the C locale, and is printed according to the current locale. For example, in a locale whose decimal point character is a comma, the command ‘printf '%g %g' 2,5 2.5’ outputs ‘2,5 2,5’. See Floating point numbers.

printf interprets ‘\ooo’ in format as an octal number (if ooo is 1 to 3 octal digits) specifying a byte to print, and ‘\xhh’ as a hexadecimal number (if hh is 1 to 2 hex digits) specifying a character to print. However, when ‘\ooo’ specifies a number larger than 255, printf ignores the ninth bit. For example, ‘printf '\400'’ is equivalent to ‘printf '\0'’.

printf interprets two syntax forms for specifying Unicode (ISO/IEC 10646) characters. ‘\u’ for 16-bit Unicode characters, specified as four hexadecimal digits hhhh, and ‘\U’ for 32-bit Unicode characters, specified as eight hexadecimal digits hhhhhhhh. printf outputs the Unicode characters according to the LC_CTYPE locale. Unicode characters in the range U+D800…U+DFFF cannot be specified by this syntax. This syntax fully supports the universal character subset introduced in ISO C 99.

The processing of ‘\u’ and ‘\U’ requires a full-featured iconv facility. It is activated on systems with glibc 2.2 (or newer), or when libiconv is installed prior to this package. Otherwise ‘\u’ and ‘\U’ will print as-is.

Unicode character syntax is useful for writing strings in a locale independent way. For example, a string containing the Euro currency symbol

$ env printf '\u20AC 14.95'

will be output correctly in all locales supporting the Euro symbol (ISO-8859-15, UTF-8, and others). Similarly, a Chinese string

$ env printf '\u4e2d\u6587'

will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).

In these examples, the printf command was invoked via env to ensure that we run the program found via your shell’s search path, and not a shell alias or a built-in function.

For larger strings, you don’t need to look up the hexadecimal code values of each character one by one. ASCII characters mixed with \u escape sequences is also known as the JAVA source file encoding. You can use GNU recode 3.5c (or newer) to convert strings to this encoding. Here is how to convert a piece of text into a shell script which will output this text in a locale-independent way:

$ LC_CTYPE=zh_TW.big5 env printf \
    '\u4e2d\u6587\n' > sample.txt
$ recode BIG5..JAVA < sample.txt \
    | sed -e "s|^|env printf '|" -e "s|%|%%|g" -e "s|$|\\\\n'|" \
    > sample.sh

The only options are a lone --help or --version. See Common options. Options must precede operands.

An exit status of zero indicates success, and a nonzero value indicates failure.