printf is a POSIX-standard command for producing formatted output in the terminal. Unlike echo, which is usually a shell builtin, printf is an external executable that you invoke each time – trading a slight performance cost for precise control over formatting, field widths, alignment, and escape sequences.
echo vs printf
When you runecho, the shell handles it internally:
printf is an external program:
Because
printf is external, the shell forks a new process to execute it. Despite this overhead, printf offers richer formatting capabilities and consistent behavior across different shells.
Format Specifiers
printf uses format specifiers similar to C’s printf(). Here are the most common ones:
| Specifier | Description | Example |
|---|---|---|
%s | String | printf "%s\n" "text" |
%d | Signed integer | printf "%d\n" 42 |
%f | Floating-point number | printf "%.2f\n" 3.14159 |
%o | Octal representation | printf "%o\n" 10 |
%x/%%X | Hexadecimal (lowercase/uppercase) | printf "%x\n" 255 |
%% | Literal percent sign | printf "%%\n" |
Example: Strings and Numbers

Escape Sequences and Portability
Different shells handleecho escape sequences inconsistently. For example:
printf which interprets \n, \t, and other escapes by default:
Avoid relying on
echo -e for escape handling across scripts. Use printf for consistent results in Bash, Dash, Zsh, and other POSIX-compliant shells.Return Status and Character Count
printf returns an exit status (0 on success) and you can pipe its output to wc -c to count characters: