Text and processing

The printf command

Prints formatted text: escapes such as \n and specifiers such as %s or %d.

Syntax

printf 'formato' [argumentos]Try it in the terminal →

Most useful options

-v varStores the result in the variable var instead of printing it. It belongs to the bash builtin, not to the external program
%sInserts the text exactly as it arrives as an argument
%dInserts a decimal integer. A value such as 3.5 causes an error
%fInserts a floating-point number; %.2f keeps only two decimals
%bLike %s, but interprets the escapes in the argument, such as \n
--helpShows the help of the external coreutils program and exits

Copy-ready examples

$ printf 'hola\n'prints with a newline at the end.

Example output

hola
$ printf '%s\n' uno dos tresrepeats the format for each argument.

Example output

uno
dos
tres
$ printf '%s|%d\n' abc 7mixes text and a number on one line.

Example output

abc|7
$ printf '%-10s|%5.2f\n' nombre 3.14159aligns columns: text left-aligned in 10 spaces, with two decimals.

Example output

nombre    | 3.14
$ printf -v numero '%05d' 42; echo "$numero"stores the result in a bash variable, zero-padded.

Example output

00042

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: printf: abc: invalid numberThe %d specifier expects an integer and received text.Pass a number, or change %d to %s if the value is text.
bash: printf: -Q: invalid optionprintf takes any argument starting with a dash before the format as an option, and Q is not a valid one.Write the format first and the dashed text after it: printf '%s\n' -Q.

Typical mistakes

  • printf does not add a newline: put \n at the end of the format if you want one.
  • The format repeats: printf '%s\n' a b c prints three lines, because the format is applied to each argument.
  • %s does not interpret escapes: printf '%s' 'a\nb' prints the backslash and the n as they are. To interpret them use %b.

Differences from similar commands

  • echo: adds the newline by itself, and how it treats escapes changes with the shell and its options. printf adds nothing and follows the same POSIX specification in any shell.
  • cat: shows the contents of files. printf writes the text you give it and reads nothing.
  • awk: processes text line by line with logic. Its printf uses a format syntax similar to this one.

Related commands

FAQ

Does printf need to be installed?

Not in bash, because it is a builtin of the shell itself. It also exists as a standalone coreutils program, so you can run /usr/bin/printf directly.

How do I print a percent sign?

Write it twice: printf '100%%\n' prints 100%.

How do I store the result in a variable?

In bash, with printf -v name 'format' argument. The variable gets the formatted text and nothing is printed to the screen.

More Text and processing commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course