$_ CLI In Depth
Learning progress 0%
lesson functions arrays parameter-expansion

Formatted Output with printf

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:27

The format string

printf takes a format string and a list of arguments. The format contains plain text and conversion specifications that begin with a percent sign. Each specification consumes one argument, converts it, and places it in the output. Nothing is added automatically: unlike echo, printf does not print a newline at the end, so you must include \n yourself.

BASH1 lines
printf "%s is %d years old\n" Ada 36

The output is Ada is 36 years old. The first conversion, %s, prints a string, and %d prints a decimal integer. Because the escape \n is in the format, the cursor moves to the next line.

Common conversions

The conversions you will use most are %s for strings, %d for integers, %f for floating-point numbers, %x and %o for hexadecimal and octal, and %% for a literal percent sign. Bash converts %f with a precision such as %.2f, which fixes the number of digits after the point.

BASH1 lines
printf "%.2f %x %o %%\n" 3.14159 255 8

The output is 3.14 ff 10 %. The number 255 is ff in hexadecimal and 8 is 10 in octal.

Width and alignment

A number between the percent sign and the conversion letter sets the minimum width. The value is right-aligned by default. A minus sign after the percent sign left-aligns it, and a zero before the width pads numbers with zeros. These options make tables that line up no matter how long the values are.

BASH2 lines
printf "%-8s|%5s\n" name qty
printf "%-8s|%5d\n" apple 3 pear 12

The output is three lines. The header is name | qty, and the rows are apple | 3 and pear | 12. The first column is padded to eight characters and the second is right-aligned to five. Every line has the same width, which is the whole point.

The format is reused

If there are more arguments than conversions, printf repeats the format for the remaining arguments. This is why the table example above works with a single format for several rows. The reuse makes it easy to print a list in pairs or triples.

BASH1 lines
printf "%s-%s\n" a b c d

The output is a-b and c-d, one pair per line, because the format was applied twice. If you give an odd number of arguments, the last line is printed with the missing argument treated as empty.

Storing output in a variable

The -v option stores the result in a variable instead of printing it. That avoids a command substitution and keeps trailing newlines under your control. It is useful when you build a string piece by piece.

BASH2 lines
printf -v line "%05.1f" 3.14159
echo "[$line]"

The output is [003.1]. The width is five, the precision is one decimal, and the zero flag fills the front with zeros.

Quoting a value for reuse

The %q conversion prints a value in a form that the shell reads back as the same value. It adds backslashes or quotes where needed. This is the right tool when you write a command line into a file, because a value with spaces or dollar signs would otherwise break the file.

BASH1 lines
printf "%q\n" "two words" plain

The output is two\ words and plain. The first value has its space escaped, so that reading it back gives one word. The second value needs no quoting, so it is printed unchanged.

Tip

Use printf instead of echo whenever the text starts with a dash, contains backslashes, or must be exact. The echo builtin handles some of those differently across shells, while printf with a quoted format is predictable.

Hands-on: a formatted report

Run the block below and check that the columns line up. Then change one width and see the effect. The lab for this unit writes a similar table to a script and checks its lines, so the widths must be exact.

BASH2 lines
printf "%-10s %6s\n" item price
printf "%-10s %6.2f\n" coffee 3.5 tea 2.25

The output is item price, then coffee 3.50, and then tea 2.25. Every value is aligned in its column, and the prices always have two decimals.

Terminal lab · 2 steps · real Linux in your browser

Print aligned columns with printf

Build an aligned table, then reuse a format and store a formatted value.

01
Print an aligned table
Create table.sh in /workspace. Print a header row with name left-aligned in eight columns and qty right-aligned in five, separated by a bar. Then print two rows, apple with 3 and pear with 12, using the same widths: ``bash cat > table.sh <<'EOF' printf "%-8s|%5s\n" name qty printf "%-8s|%5d\n" apple 3 pear 12 EOF ``
Show hint for step 1
Use %-8s for left-aligned text and %5d for a right-aligned number. The format is reused for each row.
02
Reuse a format and quote a value
Create fmt.sh in /workspace. Print the pairs a-b and c-d with one format. Store 3.14159 in a variable named line, formatted as five characters wide with one decimal and zero padding, and print it. Finally print the value two words with %q: ``bash cat > fmt.sh <<'EOF' printf "%s-%s\n" a b c d printf -v line "%05.1f" 3.14159 echo "$line" printf "%q\n" "a b" EOF ``
Show hint for step 2
printf -v stores the result in a variable. %q prints a space as a backslash followed by a space.
Check yourself Question 1 of 4
Concept Check Fixed decimal places recall

Which printf conversion prints a number with exactly two digits after the decimal point?

Recap & Key Takeaways

printf formats its arguments with a format string and does not add a newline, so include \n yourself.
Width and alignment options such as %-8s, %5d, and %05.1f make columns line up and numbers padded.
The format is reused for extra arguments, so one format can print several rows or pairs.
printf -v stores the result in a variable, and %q quotes a value so the shell reads it back unchanged.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue