printf - format and print data
| Use Case | Command | Description |
|---|---|---|
| Print formatted integer | printf '%d\n' 42 | Outputs "42" with newline |
| Print string with escape sequences | printf 'Hello\nWorld' | Prints Hello and World on separate lines |
| Print Unicode character | printf '\u20AC\n' | Prints Euro currency symbol (β¬) |
| Print with precision | printf '%.2f\n' 3.14159 | Outputs "3.14" |
printf FORMAT [ARGUMENT]...
printf does formatted printing of text. It 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 libc 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. Invoke it via env (i.e., env printf ...) to avoid interference from the shell.
printf %s a b outputs 'ab'.printf %sx%d prints 'x0'.\c, causes printf to produce no further output. For example, printf 'A%sC\cD%sF' B E prints 'ABC'.\xHH has at most two digits, unlike C. For example, printf '\x07e' prints two bytes.%b prints its argument string with \ escapes interpreted similarly to FORMAT, except octal escapes are \0OOO (0 to 3 octal digits). If a precision is given, it limits bytes printed.%q prints its argument string in a format reusable by most shells, using POSIX $'...' syntax and quoting shell metacharacters. Equivalent to ls --quoting=shell-escape.+ or -. For example, printf %.4d -3 outputs '-0003'." or ', its value is the numeric value of the following character. For example, printf "%d" "'a" outputs '97' on ASCII hosts.LC_NUMERIC and printed according to the current locale. For example, in a locale with comma as decimal point, printf '%g %g' 2,5 2.5 outputs '2,5 2,5'.printf interprets \OOO in FORMAT as an octal number (1 to 3 digits) specifying a byte, and \xHH as hexadecimal. When \OOO specifies a number >255, the ninth bit is ignored. For example, printf '\400' is equivalent to printf '\0'.
π printf interprets two character syntaxes from ISO C 99: \u for 16-bit Unicode (4 hex digits) and \U for 32-bit Unicode (8 hex digits). It outputs Unicode characters according to LC_CTYPE locale. Ranges U+0000β¦U+009F, U+D800β¦U+DFFF cannot be specified except for U+0024 ($), U+0040 (@), and U+0060 (`).
π οΈ Processing of \u and \U requires a full-featured iconv facility, activated on glibc 2.2+ or with libiconv installed. Otherwise, they print as-is.
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_CN.big5 /usr/local/bin/printf \
'\u4e2d\u6587\n' > sample.txt
$ recode BIG5..JAVA < sample.txt \
| sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
> sample.sh
The only options are a lone --help or --version. See Common options. Options must precede operands.
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. Similarly, a Chinese string:
$ env printf '\u4e2d\u6587'
will be output correctly in all Chinese locales.
Note that these examples invoke printf via env to avoid shell aliases or builtβins.
An exit status of 0 indicates success, and a nonzero value indicates failure.
Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-31 09:02 @216.73.217.152
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format