Skip to main content

How to Print Formatted Text in the Terminal with printf

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
printf '<format>' <value> ...
PartWhat it means
'<format>'The text to print, in single quotes, with placeholders in it
%sInside the format: the place where the next value goes
\nInside 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​

Terminal
printf 'Hello, %s!\n' Ada
Output
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​

Terminal
printf 'Name: %s\nCity: %s\n' Ada London
Output
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​

Terminal
name='Ada Lovelace'
city='New York'
printf 'Name: %s\nCity: %s\n' "$name" "$city"

macOS-style terminal window titled Terminal with two variables, name set to Ada Lovelace and city set to New York, and a printf command whose format puts each one on its own line.

Output
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​

Terminal
printf '%s\n' apples bananas cherries
Output
apples
bananas
cherries

macOS-style terminal window titled Terminal showing a real zsh session: printf prints Hello, Ada!, then Name and City on two lines from typed values and from variables, then apples, bananas and cherries on three lines.

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​

MistakeWhat happensDo this instead
No \n at the end: printf 'Hello, %s!' Adazsh 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 1Write %%: printf 'Done: 100%%\n' printed Done: 100%
A value with a space, not quoted: printf 'Name: %s\n' Ada LovelaceTwo values, so the format ran twice: Name: Ada and Name: LovelaceQuote the value: 'Ada Lovelace'
Fewer values than %s: printf 'Name: %s\nCity: %s\n' AdaThe second line printed as City: with nothing after itGive 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:

Terminal
printf 'Name: %s\nCity: %s\n' Ada London > card.txt
cat card.txt
Output
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​