The printf command in Linux is used to format and print data. It is similar to the printf
function in the C programming language. This command allows you to format strings, numbers, and other types of data according to a specified format.
Most of these formats, special characters are going to work with other similar commands like echo, gawk.
Note: the following examples are based on standard POSIX compliant printf command. That means they are definitely going to work with dash
shell (in Ubuntu, this is sh
shell). Some of these might not work with Bash and Zsh. All these variations mentioned.
Commands and Outputs
Printing a Simple String
Command:
printf "Hello, World!\n"
Output:
Hello, World!
Explanation:
Prints the string "Hello, World!" followed by a new line character.
Printing Special Characters using printf Command in Linux
Double Quote
Command:
printf "Quote: \"\n"
Output:
Quote: "
Explanation:
Prints the double quote character using the escape sequence \"
.
Backslash
Command:
printf "Backslash: \\"
Output:
Backslash: \
Explanation:
Prints the backslash character using the escape sequence \\
.
Alert (BEL)
Command:
printf "Alert: \a\n"
Output:
Alert: (alert sound)
Explanation:
Triggers an alert (bell) sound using the escape sequence \a
.
Backspace
Command:
printf "Backspace: \bX\n"
Output:
Backspace:X
Explanation:
Demonstrates the backspace character by moving the cursor back one space and overwriting with X
.
Form Feed
Command:
printf "Form feed: \f\n"
Output:
Form feed:
Explanation:
Prints the form feed character using the escape sequence \f
.
New Line
Command:
printf "New line: \n\n"
Output:
New line:
Explanation:
Prints two new line characters, creating an empty line.
Carriage Return
Command:
printf "Carriage return: \rCarriage\n"
Output:
Carriage return:
Explanation:
Uses the carriage return to move the cursor to the beginning of the line and overwrite the text.
Horizontal Tab
Command:
printf "Horizontal tab: \tTab\n"
Output:
Horizontal tab: Tab
Explanation:
Prints a horizontal tab character, moving the cursor to the next tab stop.
Vertical Tab
Command:
printf "Vertical tab: \vVertical\n"
Output:
Vertical tab:
Vertical
Explanation:
Prints a vertical tab character, which may not have a visible effect in modern terminals.
Octal Value
Command:
printf "Octal value: \101\n"
Output:
Octal value: A
Explanation:
Prints the character corresponding to the octal value 101
(capital A
).
Hexadecimal Value
Command:
printf "Hexadecimal value: \x41\n"
Output in bash (not in dash):
Hexadecimal value: A
Explanation:
Prints the character corresponding to the hexadecimal value 41
(capital A
).
Unicode Character (4 Digits)
Command:
printf "Unicode character (4 digits): \u03B1\n"
Output in bash:
Unicode character (4 digits): α
Explanation:
Prints the Unicode character for the Greek small letter alpha using a 4-digit hex code.
Unicode Character (8 Digits)
Command:
printf "Unicode character (8 digits): \U000003B1\n"
Output in bash:
Unicode character (8 digits): α
Explanation:
Prints the Unicode character for the Greek small letter alpha using an 8-digit hex code.
Percent Sign
Command:
printf "Percent sign: %%\n"
Output:
Percent sign: %
Explanation:
Prints a literal percent sign by escaping it with another percent sign.
Formatting with Flags
Interpret Backslash Escapes
Command:
printf "%b\n" "String with new line \n"
Output:
String with new line
Explanation:
Interprets backslash escapes in the argument, printing a new line where \n
is present.
Shell-Input Format
Command:
printf "%q\n" "String with $ and '"
Output in bash:
String\ with\ \$\ and\ \'
Explanation:
Formats the argument for safe reuse as shell input, escaping special characters.
Formatting Numbers
Integer
Command:
printf "Integer: %d\n" 42
Output:
Integer: 42
Explanation:
Prints an integer in decimal format.
Unsigned Integer
Command:
printf "Unsigned integer: %u\n" 42
Output:
Unsigned integer: 42
Explanation:
Prints an unsigned integer in decimal format.
Octal Integer
Command:
printf "Octal integer: %o\n" 42
Output:
Octal integer: 52
Explanation:
Prints an integer in octal format.
Hexadecimal Integer (Lowercase)
Command:
printf "Hexadecimal integer (lowercase): %x\n" 42
Output:
Hexadecimal integer (lowercase): 2a
Explanation:
Prints an integer in hexadecimal format using lowercase letters.
Hexadecimal Integer (Uppercase)
Command:
printf "Hexadecimal integer (uppercase): %X\n" 42
Output:
Hexadecimal integer (uppercase): 2A
Explanation:
Prints an integer in hexadecimal format using uppercase letters.
Floating Point (Lowercase)
Command:
printf "Floating point (lowercase): %f\n" 42.42
Output:
Floating point (lowercase): 42.420000
Explanation:
Prints a floating-point number in decimal format with six digits after the decimal point.
Floating Point (Uppercase)
Command:
printf "Floating point (uppercase): %F\n" 42.42
Output:
Floating point (uppercase): 42.420000
Explanation:
Similar to %f
, but uses uppercase letters for scientific notation.
Scientific Notation (Lowercase)
Command:
printf "Scientific notation (lowercase): %e\n" 42.42
Output:
Scientific notation (lowercase): 4.242000e+01
Explanation:
Prints a floating-point number in scientific notation with lowercase e
.
Scientific Notation (Uppercase)
Command:
printf "Scientific notation (uppercase): %E\n" 42.42
Output:
Scientific notation (uppercase): 4.242000E+01
Explanation:
Prints a floating-point number in scientific notation with uppercase E
.
Shortest Representation
Command:
printf "Shortest representation: %g\n" 42.42
Output:
Shortest representation: 42.42
Explanation:
Prints a floating-point number in the shortest format, either normal or scientific notation.
Shortest Representation (Uppercase)
Command:
printf "Shortest representation (uppercase): %G\n" 42.42
Output:
Shortest representation (uppercase): 42.42
Explanation:
Similar to %g
, but uses uppercase letters for scientific notation.
String
Command:
printf "String: %s\n" "Hello, World!"
Output:
String: Hello, World!
Explanation:
Prints the string "Hello, World!" as is.
Conclusion
The printf
command is a powerful tool for formatting output in Linux. By using various format specifiers and escape sequences, you can control the appearance of your output and make it suitable for different purposes. This makes it highly useful in writing scripts. To learn about scripts, look here. Whether you're formatting strings, numbers, or special characters, printf
provides a flexible way to present data in your scripts and terminal sessions.