Linux printf Command
Complete Linux Command Reference
1. printf Command Overview
printf is a powerful formatted output command in Linux/Unix systems, derived from the printf() function in C language. Compared with the echo command, printf provides more precise output control and formatting capabilities.
Key Features
- Precise formatting: Controls output alignment, width, precision, etc.
- No automatic newline: Does not add a newline at the end of the output by default (unlike echo)
- Multi-language support: Supports Unicode character output
- Variable insertion: Can insert variable values into strings
2. Basic Syntax
printf format-string [arguments...]
Parameter Description
- format-string: Format string, containing regular characters and format specifiers
- arguments: Argument list corresponding to the format specifiers
3. Detailed Explanation of Format Specifiers
Format specifiers start with%; the basic form is:
%[flags][width][.precision]specifier
Common Format Specifiers
| Specifier | Description | Example |
|---|---|---|
| %s | String | printf "%s" "hello" |
| %d | Decimal integer | printf "%d" 123 |
| %f | Floating-point number | printf "%f" 3.14 |
| %x | Hexadecimal integer (lowercase) | printf "%x" 255 |
| %X | Hexadecimal integer (uppercase) | printf "%X" 255 |
| %o | Octal integer | printf "%o" 8 |
| %c | Single character | printf "%c" 65 |
Modifier Options
1. Flags
| Flag | Description | Example |
|---|---|---|
| - | Left-align | printf "%-10s" "hi" |
| + | Display plus/minus sign | printf "%+d" 123 |
| 0 | Pad with zeros | printf "%05d" 12 |
| Space | Leave a space before positive numbers | printf "% d" 123 |
| # | Special format (e.g., 0x prefix) | printf "%#x" 255 |
2. Width
Specifies the minimum field width, pads when insufficient
Example
printf "%-10s\n" "hello" # Left-aligned, width 10
3. Precision (.precision)
For floating-point numbers, specifies the number of digits after the decimal point; for strings, specifies the maximum number of characters
Example
printf "%.5s\n" "abcdefg" # Output abcde
4. Practical Examples
Example 1: Basic Formatted Output
Example
Output:
Name: Alice, Age: 25, Height: 1.68
Example 2: Table-aligned Output
Example
printf "%-10s %-10d %-10.2f\n" "Alice" 25 89.5
printf "%-10s %-10d %-10.2f\n" "Bob" 23 92.3
Output:
Name Age Score Alice 25 89.50 Bob 23 92.30
Example 3: Special Character Handling
Example
printf "Path: %s\n" "/home/user"
printf "Alert: \a\n" # Bell character
Example 4: Variable Insertion
Example
age=30
printf "User: %s, %d years old\n" "$name" "$age"
5. Advanced Usage
1. Formatting Date Output
Example
2. Color Output
Example
printf "\e[32mGreen Text\e[0m\n"
3. Dynamically Specifying Width
Example
printf "%*s\n" $width "Right aligned"
6. Common Problems and Considerations
Newline issue: printf does not add a newline by default; it needs to be added manually\n
Example
printf "Add newline\n"
Argument count mismatch: When there are fewer arguments than format specifiers, undefined values will be output
Example
Quotation handling: It is recommended to enclose the format string in double quotes, and also use double quotes for variables
Example
Escaping special characters: Special characters need to be escaped with backslashes in the format string
7. printf vs echo
| Feature | printf | echo |
|---|---|---|
| Newline | Must explicitly add \n | Added automatically |
| Formatting | Supports complex formatting | Simple output |
| Consistency | Consistent behavior across systems | May differ across systems |
| Variable expansion | Must be explicitly specified | Automatic expansion |
| Special characters | Need escaping | Some versions support the -e option |