Home·Terminal commands

printf

Text processing

Usage

printf '[format]' [args]

Formats text from a template exactly like C's printf; it adds no newline of its own and reuses the format from the start while arguments remain.

Common flags

FlagMeaning
%sInsert a string
%dInsert a whole number
%.2fA number with exactly two decimals
%-10sA string padded to ten columns, left aligned
\nA newline; printf never adds one for you

Examples

printf '%s\t%d\n' apple 5

One tab-separated line.

printf '%.2f\n' 3.14159

Rounds to two decimals.

printf '%s\n' a b c

The format repeats, so this prints three lines.

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does printf do?

Formats text from a template exactly like C's printf; it adds no newline of its own and reuses the format from the start while arguments remain.

Q. How do I type it?

printf '[format]' [args] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man printf. This command sits under Text processing.

Related commands

man page: man printf