Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
printf FORMAT [ARGUMENT]...

Try it

Bash Specifiers, width and precision

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.

Bash Reusing the format, escapes and pitfalls

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

Terminal
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. Use printf '%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 \n yourself.
  • Numbers: a leading 0 is octal, 0x hexadecimal, and 'A (a quote and a character) gives the character code. An invalid number such as abc for %d gives an error message, prints 0 and returns non-zero.
  • Floats and locale: the decimal separator follows LC_NUMERIC. Set LC_ALL=C in 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 bc or awk with care.
  • %s with a number argument prints it as given; %d with a decimal like 3.7 is an error. Use %.0f to round.
  • Builtin extras: printf -v, %q and %(...)T avoid starting processes and are portable to Bash only.
  • Portability: use %s, %d, %f, widths and \n for POSIX scripts; skip %b, %q and -v.