Linux Command Reference ยท Text Processing
Linux printf command
Reference for the Linux printf command: format specifiers, padding and alignment, reusing the format for many arguments, escapes, %q and safe printing of data.
printf prints text according to a format string, just like the C function of the same name. The format contains ordinary text, backslash escapes and conversion specifiers such as %s (string), %d (integer) or %5.2f (number with width and precision); each specifier consumes one argument.
It is the reliable alternative to echo: it never mistakes your data for an option, it does not add a newline unless you ask, and it behaves the same in every shell. There is a Bash builtin and an external /usr/bin/printf; the builtin has a few extras such as -v and %q.
printf FORMAT [ARGUMENT]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
printf [-v VAR] FORMAT [ARGUMENT]...
A specifier has the form %[flags][width][.precision]type.
| Part | Meaning |
|---|---|
%s |
String. |
%d, %i |
Signed decimal integer. |
%u, %o, %x, %X |
Unsigned decimal, octal, lower or upper hexadecimal. |
%f, %e, %g |
Floating point: fixed, exponent, shortest. |
%c |
First character of the argument. |
%b |
String with backslash escapes expanded (builtin and GNU). |
%q |
String quoted so the shell reads it back as one word (Bash builtin). |
%% |
A literal percent sign. |
%(fmt)T |
Bash builtin: format a Unix time with strftime; -1 means now. |
| flags | - left-align, 0 zero-pad, + always show sign, space, # alternate form. |
| width | Minimum field width, or * to take it from an argument. |
.precision |
Digits after the point for floats; maximum characters for %s. |
-v VAR |
Bash builtin: store the result in VAR instead of printing it. |
Escapes in the format: \n, \t, \\, \", \a, \b, \r, \0NNN (octal), \xHH.
Returns: 0 on success; non-zero on invalid numbers or format errors (printing still happens for the valid parts).
Usage notes
- Never put data in the format.
printf "$user_text"treats%and backslashes in the text as directives. Useprintf '%s\n' "$user_text". - The format is reused for as many rounds as are needed to consume all arguments, which makes it a neat looping tool. With no arguments, it runs once with empty or zero values.
- No automatic newline. Add
\nyourself. - Numbers: a leading
0is octal,0xhexadecimal, and'A(a quote and a character) gives the character code. An invalid number such asabcfor%dgives an error message, prints 0 and returns non-zero. - Floats and locale: the decimal separator follows
LC_NUMERIC. SetLC_ALL=Cin scripts that must always print a point. - Width counts bytes in many implementations, so padding multi-byte text can misalign columns.
- Large integers are handled up to 64 bits; beyond that use
bcorawkwith care. %swith a number argument prints it as given;%dwith a decimal like3.7is an error. Use%.0fto round.- Builtin extras:
printf -v,%qand%(...)Tavoid starting processes and are portable to Bash only. - Portability: use
%s,%d,%f, widths and\nfor POSIX scripts; skip%b,%qand-v.