Linux and Unix printf command

Quick links

About printf
Syntax
Examples
Related commands
Linux and Unix main page

About printf

printf inserts arguments into a user-defined string of text, creating formatted output.

Syntax

printf FORMAT [ARGUMENT]...
printf OPTION

Description

printf prints a formatted string to the standard output. Its roots are in the C programming language, which uses a function by the same name. It is a handy way to produce precisely-formatted output from numerical or textual arguments.

Options

FORMAT FORMAT controls the output, and defines the way that the ARGUMENTs will be expressed in the output. See the Format section, below.
ARGUMENT Each ARGUMENT will be inserted into the formatted output according to the definition of FORMAT.
--help Display a help mesage, and exit.
--version Display version information, and exit.

Format

The FORMAT string contains three types of objects:

  • ordinary characters, which are copied verbatim to the output.
  • interpreted character sequences, which are escaped with a backslash ("\").
  • conversion specifications, which define the way in which ARGUMENTs will be expressed as part of the output.

Here is a quick example which uses these three types of objects:

printf "My name is \"%s\".\nIt's a pleasure to meet you." "John"

This command produces the output:

My name is "John". It's a pleasure to meet you.

Here, FORMAT is enclosed in double-quotes ("). There is one conversion specification: %s, which interprets the argument "John" as a string and inserts it into the output. There are three escaped character sequences: two occurrences of \" and one occurrence of \n. The sequence \" translates as a literal double-quote; it is escaped with a backslash so that printf knows to treat it as a literal character, and not as the end of the FORMAT string. \n is the sequence for a newline character, and tells printf to begin a new line and continue the output from there.

The power of printf lies in the fact that for any given FORMAT string, the ARGUMENTs can be changed to affect the output. For example, the output of the command in the above example can be altered just by changing the argument, "John". If used in a script, this argument can be set to a variable. For instance, the command

printf "Hi, I'm %s.\n" $LOGNAME

...will insert the value of the environment variable $LOGNAME, which is the username of whoever ran the command.

Conversion Specifications

Each conversion specification begins with a % and ends with a conversion character. Between the % and the conversion character there may be, in order:

- A minus sign. This tells printf to left-adjust the conversion of the argument.
number An integer that specifies field width; printf will print a conversion of ARGUMENT in a field at least number characters wide. If necessary it will be padded on the left (or right, if left-adjustment is called for) to make up the field width.
. A period, which separates the field width from the precision.
number An integer, the precision, which specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer.
h or l These differentiate between a short and a long integer, respectively, and are generally only needed for computer programming.

The conversion characters themselves, which tell printf what kind of argument to expect, are as follows:

conversion character argument type
d, i An integer, expressed as a decimal number.
o An integer, expressed as an unsigned octal number.
x, X An integer, expressed as an unsigned hexadecimal number
u An integer, expressed as an unsigned decimal number.
c An integer, expressed as a character. The integer corresponds to the character's ASCII code.
s A string.
f A floating-point number, with a default precision of 6.
e, E A floating-point number expressed in scientific notation, with a default precision of 6.
p A memory address pointer.
% No conversion; a literal percent sign ("%") is printed instead.

A width or precision may be represented with an asterisk ("*"); if so, the asterisk reads in an argument, which must be an integer, and uses that value. For example,

printf "%.*s" 5 "abcdefg"

...produces the following output:

abcde

The following table represents the the way that printf would output its ARGUMENT, "computerhope", using various FORMAT strings. Each string is enclosed in quotes so that it's easier to see the exact extent of each:

FORMAT string ARGUMENT string output string
"%s" "computerhope"
"computerhope"
"%8s" "computerhope"
"computerhope"
"%.8s" "computerhope"
"computer"
"%-8s" "computerhope"
"computerhope"
"%-15s" "computerhope"
"computerhope "
"%15.8s" "computerhope"
" computer"
"%-15.8" "computerhope"
"computer "
"%-15.2" "computerhope"
"co "

Please note that printf requires the number of conversion strings to match the number of ARGUMENTs; it maps them one-to-one, and expects to find exactly one ARGUMENT for each conversion string. The only exception is a conversion string which uses an asterisk; such strings require two arguments each.

Conversion strings are always interpreted from left to right. For example, the following printf command:

printf "%d plus %5f %s %.*f." 5 5.05 "equals" 3 10.05

...produces the following output:

5 plus 5.050000 equals 10.050.

Interpreted Escaped Character Sequences

The following character sequences are interpreted as special characters by printf:

\" prints a double-quote (")
\\ prints a backslash (\)
\a issues an alert (plays a bell)
\b prints a backspace
\c instructs printf to produce no further output
\e prints an escape character (ASCII code 27)
\f prints a form feed
\n prints a newline
\r prints a carriage return
\t prints a horizontal tab
\v prints a vertical tab
\NNN prints a byte with octal value NNN (1 to 3 digits)
\xHH prints a byte with hexadecinal value HH (1 to 2 digits)
\uHHHH prints the unicode character with hexadecimal value HHHH (4 digits)
\UHHHHHHHH prints the unicode character with hexadecimal value HHHHHHHH (8 digits)
%b prints ARGUMENT as a string with "\" escapes interpreted as listed above, with the exception that octal escapes take the form \0 or \0NN

Quoting In The Shell

Be careful with the way your shell interprets quoted strings. If your shell is not interpreting your quoted string correctly, try using single-quotes rather than double-quotes.

Examples

printf 'hello\nworld\n!'

Prints the following output:

hello 
world 
!
printf "%b" 'hello\nworld\n!'

Prints the same output as the above example.

printf "Your home folder is %s.\n" $HOME

Prints a string telling you the location of your home directory.

Related commands

awk — Interpreter for the AWK text processing programming language.
bc — A calculator.
echo — Output text.