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.
printf "%s is %d years old\n" Ada 36The 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.
printf "%.2f %x %o %%\n" 3.14159 255 8The 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.
printf "%-8s|%5s\n" name qty
printf "%-8s|%5d\n" apple 3 pear 12The 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.
printf "%s-%s\n" a b c dThe 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.
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.
printf "%q\n" "two words" plainThe 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.
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.
printf "%-10s %6s\n" item price
printf "%-10s %6.2f\n" coffee 3.5 tea 2.25The 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.