Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux printf command

Linux printf command

Linux Command Reference Linux Command Reference


I. Overview of the printf command

printf is a powerful formatted output command in Linux/Unix systems; it originates from the printf() function in the C language. Compared with the echo command, printf provides more precise output control and formatting capabilities.

Main Features


II. Basic syntax



printf format-string [arguments...]

Parameter Description


III. Format specifiers in detail

Format specifiers begin with % and basically take the form:

%[flags][width][.precision]specifier

Common format specifiers

Specifier Description Example
%s String printf "%s" "hello"
%d Decimal integer printf "%d" 123
%f Float (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 the sign printf "%+d" 123
0 Pad with zeros printf "%05d" 12
Space Leave a space before positive numbers printf "% d" 123
# Special format (such as the 0x prefix) printf "%#x" 255

2. Width

Specify the minimum field width, padding when insufficient

Example

printf

"%10s\n"

"hello"

 

Right align, width 10

printf

"%-10s\n"

"hello"

Left align, width 10

3. Precision (.precision)

For floating-point numbers, specify the number of digits after the decimal point; for strings, specify the maximum number of characters

Example

printf

"%.2f\n"

3.14159

 

Output 3.14

printf

"%.5s\n"

"abcdefg"

Output abcde


IV. Practical examples

Example 1: Basic formatted output

Example

printf

"Name: %s, Age: %d, Height: %.2f\n"

"Alice"

25

1.68

Output:

Name: Alice, Age: 25, Height: 1.68

Example 2: Table-aligned output

Example

printf

"%-10s %-10s %-10s\n"

"Name"

"Age"

"Score"

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: Handling special characters

Example

printf

"Temperature: %d°C\n"

25

printf

"Path: %s\n"

"/home/user"

printf

"Alert: \a\n"

 

Bell character

Example 4: Variable insertion

Example

name

=

"John"

age

=

30

printf

"User: %s, %d years old\n"

"$name"

"$age"


V. Advanced usage

1. Formatting date output

Example

printf

"Today is %(%Y-%m-%d)T\n"

-1

2. Color output

Example

printf

"\e[31mRed Text\e[0m\n"

printf

"\e[32mGreen Text\e[0m\n"

3. Dynamically specifying width

Example

width

=

20

printf

"%*s\n"

$width

"Right aligned"


VI. Common issues and notes

Newline issues: printf does not add a newline by default; you need to add it manually \n

Example

printf

"No newline"

printf

"Add newline\n"

Mismatched number of arguments: when there are fewer arguments than format specifiers, undefined values are output

Example

printf

"%s %s\n"

"only_one"

 

The second %s will output empty

Quote handling: it is recommended to enclose the format string in double quotes, and to use double quotes for variables as well

Example

printf

"%s\n"

"$variable"

Escaping special characters: you need to use backslash escapes for special characters in the format string


VII. 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 between systems
Variable expansion Must be specified explicitly Automatic expansion
Special characters Requires escaping Some versions support the -e option

Linux Command Reference Linux Command Reference