On Unix-like operating systems, the printf command inserts arguments into a user-defined string of text, creating formatted output.

This page covers the GNU/Linux version of printf.

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.

  • Description
  • Syntax
  • Format
  • Conversion specifications
  • Interpreted escaped character sequences
  • Quoting in the shell
  • Examples
  • Related commands
  • Linux commands help

Syntax

printf FORMAT [ARGUMENT]…

printf OPTION

Options

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 the 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 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:

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

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 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:

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

5 plus 5.050000 equals 10.050.

Interpreted escaped character sequences

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

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.

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