How to Print Formatted Text in the Terminal with printf
printf prints text in a layout you set: a format with %s where each value goes and \n where each line ends. It prints exactly what the format says, and it does it the same way in bash and zsh.
This guide prints a name and a city on two lines, with real output from zsh 5.9 and bash 3.2 on macOS.
Syntax
printf '<format>' <value> ...
| Part | What it means |
|---|---|
'<format>' | The text to print, in single quotes, with placeholders in it |
%s | Inside the format: the place where the next value goes |
\n | Inside the format: the end of a line |
%% | Inside the format: a percent sign |
<value> ... | The values, one for each %s, separated by spaces |
The bash manual describes the command in one line: Write the formatted arguments to the standard output under the control of the format. And the format itself: The format is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument.
%s is a format specification, and \n is an escape sequence. The zsh manual says The same escape sequences as for echo are recognised in the format., and its list for echo gives \n as linefeed (newline).
The Example
You print a greeting, then a name and a city on two lines, first from values you type and then from two variables, and last a list with one item on each line.
Step 1: Put One Value in a Sentence
printf 'Hello, %s!\n' Ada
Hello, Ada!
printf copied Hello, , put Ada where %s was, copied the !, and ended the line at \n. The single quotes hand the format to printf exactly as typed, backslash included. The bash manual: Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes.
Step 2: Print Two Lines with One Command
printf 'Name: %s\nCity: %s\n' Ada London
Name: Ada
City: London
The first %s took the first value and the second %s took the second. Each \n ended a line, so one command printed two.
Step 3: Take the Values from Variables
name='Ada Lovelace'
city='New York'
printf 'Name: %s\nCity: %s\n' "$name" "$city"

Name: Ada Lovelace
City: New York
Both values have a space in them, so the variables are in double quotes. The shell still puts each variable's value in. The zsh manual: Inside double quotes (""), parameter and command substitution occur. Each value reached printf in one piece: Ada Lovelace filled the first %s and New York the second.
Step 4: Print a List, One Item on Each Line
printf '%s\n' apples bananas cherries
apples
bananas
cherries

The format has one %s and there are three values, so printf used the format three times. The bash manual: The format is reused as necessary to consume all of the arguments. The zsh manual says the same: If arguments remain unused after formatting, the format string is reused until all arguments have been consumed. bash printed the same lines in every step.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No \n at the end: printf 'Hello, %s!' Ada | zsh printed Hello, Ada!%, and bash put its next prompt straight after Hello, Ada! | End the format with \n |
A single % in the text: printf 'Done: 100%\n' | zsh: printf: %\n: invalid directive. bash: bash: printf: `\': invalid format character. Both printed only Done: 100, with no line break, and the exit status was 1 | Write %%: printf 'Done: 100%%\n' printed Done: 100% |
A value with a space, not quoted: printf 'Name: %s\n' Ada Lovelace | Two values, so the format ran twice: Name: Ada and Name: Lovelace | Quote the value: 'Ada Lovelace' |
Fewer values than %s: printf 'Name: %s\nCity: %s\n' Ada | The second line printed as City: with nothing after it | Give one value for each %s |
The % after Hello, Ada! isn't part of the output. The zsh manual: When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a ‘%’ for a normal user or a ‘#’ for root. The last row is the documented behaviour: If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied.
FAQ
Why use printf and not echo?
Because echo treats \n differently in the two shells. echo 'Name: Ada\nCity: London' printed two lines in zsh, but Name: Ada\nCity: London on one line in bash. printf printed the same two lines in both. The zsh manual points the same way: For a more portable way of printing text, see printf.
Which printf runs?
The shell's own. type printf printed printf is a shell builtin in both zsh and bash.
How do I put the lines in a file?
Add > and a file name after the command:
printf 'Name: %s\nCity: %s\n' Ada London > card.txt
cat card.txt
Name: Ada
City: London
printf printed nothing on the screen, and cat showed the two lines in card.txt. The bash manual on >: If the file does not exist it is created; if it does exist it is truncated to zero size.
Conclusion
printf 'Name: %s\nCity: %s\n' "$name" "$city" printed Name: Ada Lovelace and City: New York on two lines, the same in zsh and bash. Keep the format in single quotes, end each line with \n, write %% for a percent sign, and give one value for each %s.
Additional Resources
- Bash manual: Bash Builtin Commands:
printfand its format. - zsh manual: Shell Builtin Commands:
printf, and the escape sequences it shares withecho.
