printf Command
Intermediate Shell Scripting man(1)Format and print data with precise control
👁 11 views
📅 Updated: Mar 15, 2026
SYNTAX
printf FORMAT [ARGUMENT]...
What Does printf Do?
printf formats and prints data according to a format string, similar to C\'s printf function. It provides precise control over output formatting including field widths, padding, number formatting, and escape sequences.
printf is more consistent than echo across different shells and POSIX systems. It does not add a trailing newline by default, giving you complete control over output. It supports format specifiers: %s (string), %d (integer), %f (float), %x (hex).
printf is the recommended choice for formatted output in shell scripts, especially when portability matters. It handles escape sequences consistently without needing flags like echo -e.
printf is more consistent than echo across different shells and POSIX systems. It does not add a trailing newline by default, giving you complete control over output. It supports format specifiers: %s (string), %d (integer), %f (float), %x (hex).
printf is the recommended choice for formatted output in shell scripts, especially when portability matters. It handles escape sequences consistently without needing flags like echo -e.
Options & Flags
| Option | Description | Example |
|---|---|---|
| %s | String format specifier | printf "%s\n" "Hello" |
| %d | Integer format specifier | printf "%d\n" 42 |
| %f | Float format specifier | printf "%.2f\n" 3.14159 |
| %x | Hexadecimal format specifier | printf "%x\n" 255 |
| %-Ns | Left-align in N-width field | printf "%-20s %s\n" "Name" "Value" |
| %0Nd | Zero-padded number | printf "%05d\n" 42 |
Practical Examples
#1 Formatted string
Formats and prints with type-safe specifiers.
$ printf "Name: %s, Age: %d\n" "Alice" 30
Output:
Name: Alice, Age: 30
#2 Formatted table
Creates aligned table output with fixed-width columns.
$ printf "%-15s %-10s %s\n" "Name" "Role" "Dept"; printf "%-15s %-10s %s\n" "Alice" "Dev" "Engineering"
Output:
Name Role Dept
Alice Dev Engineering
#3 Zero-padded numbers
Generates zero-padded filenames: file_001.txt through file_005.txt.
$ for i in $(seq 1 5); do printf "file_%03d.txt\n" $i; done
Output:
file_001.txt
file_002.txt
file_003.txt
#4 Float formatting
Formats a number with exactly 2 decimal places.
$ printf "Price: $%.2f\n" 49.9
Output:
Price: $49.90
#5 Hex conversion
Converts a number to decimal, hexadecimal, and octal.
$ printf "Decimal: %d, Hex: 0x%x, Octal: %o\n" 255 255 255
Output:
Decimal: 255, Hex: 0xff, Octal: 377
#6 Repeat format for multiple args
printf repeats the format string for each argument.
$ printf "%s\n" apple banana cherry
Output:
apple
banana
cherry
Tips & Best Practices
No newline by default: printf does not add \n automatically. Always include \n in your format string: printf "%s\n" "text".
Reusable format: printf reuses the format string for extra arguments: printf "%s," a b c outputs a,b,c, — great for building CSV.
Quote the format string: Always quote the format string to prevent shell expansion: printf "%s\n" "$var". Without quotes, special characters may be interpreted.
Frequently Asked Questions
When should I use printf instead of echo?
Use printf for formatted output, portable scripts, precise number formatting, and when you need consistent behavior across different shells.
How do I format numbers with printf?
%d for integers, %.2f for 2 decimal places, %05d for zero-padded, %x for hex. Example: printf "%.2f\n" 3.14159 outputs 3.14.
Why does printf not add a newline?
By design — it gives you complete control. Always add \n explicitly: printf "%s\n" "text".
Related Commands
More Shell Scripting Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →