LinuxCommandLibrary

printf

Format and print output

TLDR

Print a text message

$ printf "[%s\n]" "[Hello world]"
copy

Print an integer in bold blue
$ printf "[\e[1;34m%.3d\e[0m\n]" [42]
copy

Print a float number with the Unicode Euro sign
$ printf "[\u20AC %.2f\n]" [123.4]
copy

Print a text message composed with environment variables
$ printf "[var1: %s\tvar2: %s\n]" "[$VAR1]" "[$VAR2]"
copy

Store a formatted message in a variable (does not work on Zsh)
$ printf -v [myvar] ["This is %s = %d\n" "a year" 2016]
copy

Print a hexadecimal, octal and scientific number
$ printf "[hex=%x octal=%o scientific=%e]" 0x[FF] 0[377] [100000]
copy

SYNOPSIS

printf FORMAT [ARGUMENT...]

PARAMETERS

FORMAT
    The primary control string that defines the output format. It can include literal characters, escape sequences (e.g., \n, \t), and format specifiers (e.g., %s, %d) that indicate where and how the subsequent ARGUMENTs should be inserted and formatted.

ARGUMENT...
    One or more values (numbers, strings) that correspond to the format specifiers in the FORMAT string. These arguments are formatted and printed in the order they appear, according to their respective specifiers. If there are more arguments than specifiers, the FORMAT string is reused. If there are fewer, missing arguments for numeric specifiers typically result in 0 or an empty string for string specifiers.

Common Format Specifiers:
    These are placed within the FORMAT string and begin with a percent sign (%), indicating the type and formatting of the corresponding argument.

%s
    Prints a string.

%d, %i
    Prints a signed decimal integer.

%u
    Prints an unsigned decimal integer.

%o
    Prints an unsigned octal integer.

%x, %X
    Prints an unsigned hexadecimal integer (lowercase and uppercase respectively).

%f, %F
    Prints a floating-point number in decimal notation.

%e, %E
    Prints a floating-point number in scientific (exponential) notation.

%g, %G
    Prints a floating-point number using either %f/%F or %e/%E format, whichever is more compact.

%c
    Prints a single character.

%%
    Prints a literal percent sign.

Common Escape Sequences:
    These special character sequences are used within the FORMAT string to represent non-printable or special characters.

\n
    Newline character.

\t
    Horizontal tab character.

\\
    Literal backslash character.

\b
    Backspace character.

\r
    Carriage return character.

\f
    Form feed character.

\v
    Vertical tab character.

\a
    Alert (bell) character.

\0nnn
    Character whose ASCII code is the one- to three-digit octal value nnn.

\xhh
    Character whose ASCII code is the one- or two-digit hexadecimal value hh.

DESCRIPTION

The printf command is a powerful utility in Linux and Unix-like systems, widely used for precise formatting and printing of text and data. It emulates the behavior of the printf function from the C programming language, providing extensive control over how output is displayed.

Unlike echo, printf does not automatically append a newline character to the output, requiring explicit use of \n. This fine-grained control makes it invaluable in shell scripting for generating structured reports, custom messages, or data files with specific alignments, padding, and numerical representations (e.g., decimal, hexadecimal, scientific notation). It interprets a FORMAT string that contains literal text, escape sequences, and format specifiers, substituting corresponding ARGUMENTs into the output based on these specifiers.

CAVEATS

Unlike echo, printf does not automatically add a newline character at the end of its output. A \n escape sequence must be explicitly included in the FORMAT string for a newline to be printed.

The behavior of printf can vary slightly depending on whether it's a shell built-in (e.g., in Bash, Zsh) or an external utility, though core functionality remains consistent.

If there are more arguments than format specifiers in the FORMAT string, printf will reuse the FORMAT string from the beginning for the remaining arguments. If there are fewer arguments, any unspecified format specifiers for numbers typically print 0, and for strings, an empty string.

FIELD WIDTH AND PRECISION

printf offers advanced formatting options through modifiers within format specifiers. For example, %10s allocates a minimum width of 10 characters for a string, padding with spaces if necessary. %-10s left-justifies the string within that width. For numbers, %.2f formats a floating-point number to two decimal places, while %05d pads an integer with leading zeros to a width of 5 digits. Combined, %width.precisiontype offers fine-grained control over output appearance.

HISTORY

The printf command originated from the printf() function in the C programming language, which has been a fundamental part of C since its early development. Its powerful and flexible text formatting capabilities made it an essential addition to Unix-like operating systems and later to shell scripting languages. It provides a more robust and controllable alternative to simpler print commands, especially when precise output alignment, numerical precision, or specific data representations are required. Its inclusion as a standard utility and shell built-in reflects its widespread utility in system administration and scripting.

SEE ALSO

echo(1), awk(1), gawk(1), xargs(1), fmt(1)

Copied to clipboard