- Article pages now have a discussion option at the bottom (moderated/captcha, but no registration needed)

This is an old revision of the document!


The printf command

FIXME incomplete FIXME Stranger, this is a very big topic that needs experience - please extend the descriptions and correct the details if you can!

Attention: This is about the Bash-builtin command printf - however, the description should be nearly identical for an external command that follows POSIX®.

Unlike other documentations, I don't want to redirect you to the manual page for the printf() C function family. However, if you're more experienced, that should be the most detailed description for the format strings and modifiers.

Due to mutual exclusive historical implementations of the echo command, POSIX® recommends to use printf rather than echo.

General

The printf command provides a method to print preformatted text similar to the printf() system interface (C function). It's meant as successor for echo and has far more features and possibilities.

Beside other reasons, POSIX® has a very good argument to recommend it: Both historical main flavours of the echo command are mutual exclusive, they collide. A "new" command had to be invented to solve the issue.

Syntax

printf <FORMAT> <ARGUMENTS...>

The text format is given in <FORMAT>, while all arguments the formatstring may point to are given after that, here, indicated by <ARGUMENTS…>.

Thus, a typical printf-call looks like:

printf "Surname: %s\nName: %s\n" "$SURNAME" "$LASTNAME"
where "Surname: %s\nName: %s\n" is the format specification, and the two variables are passed as arguments, the %s in the formatstring points to (for every format specifier you give, printf awaits one argument!).

GNU Awk expects a comma after the format string and between each of the arguments of a printf command. For examples, see: code snippet.

Options

-v VARIf given, the output is assigned to the variable VAR instead of printed to stdout (comparable to sprintf() in some way)

The -v Option can't assign directly to array indexes in Bash versions older than Bash 4.1.

In versions newer than 4.1, one must be careful when performing expansions into the first non-option argument of printf as this opens up the possibility of an easy code injection vulnerability.
$ var='-vx[$(echo hi >&2)]'; printf "$var" hi; declare -p x
hi
declare -a x='([0]="hi")'
…where the echo can of course be replaced with any arbitrary command. If you must, either specify a hard-coded format string or use -- to signal the end of options. The exact same issue also applies to read, and a similar one to mapfile, though performing expansions into their arguments is less common.

Arguments

Of course in shell-meaning the arguments are just strings, however, the common C-notations plus some additions for number-constants are recognized to give a number-argument to printf:

Number-FormatDescription
NA normal decimal number
0NAn octal number
0xNA hexadecimal number
0XNA hexadecimal number
"X(a literal double-quote infront of a character): interpreted as number (underlying codeset) don't forget escaping
'X(a literal single-quote infront of a character): interpreted as number (underlying codeset) don't forget escaping

If more arguments than format specifiers are present, then the format string is re-used until the last argument is interpreted. If fewer format specifiers than arguments are present, then number-formats are set to zero, while string-formats are set to null (empty).

Also, to minimize surprises, when printf expects an argument, give it one, not more. I'm talking about shell word splitting, please read this article if you don't know what I mean.

Again, attention: When a numerical format expects a number, the internal printf-command will use the common Bash arithmetic rules regarding the base. A command like the following example will throw an error, since 08 is not a valid octal number (00 to 07!):
printf "%d\n" 08

Format strings

FIXME incomplete

The format string interpretion is derived from the C printf() function family. Only format specifiers that end in one of the letters diouxXfeEgGcs are recognized.

To print a literal % (percent-sign), use %% in the format string.

Again: Every format specifier expects an associated argument provided!

These specifiers have different names, depending who you ask. But they all mean the same: A placeholder for data with a specified format:

  • format placeholder
  • conversion specification
  • formatting token
Format Description
%b Print the associated argument while interpreting backslash escapes in there
%q Print the associated argument shell-quoted, reusable as input
%d Print the associated argument as signed decimal number
%i Same as %d
%o Print the associated argument as unsigned octal number
%u Print the associated argument as unsigned decimal number
%x Print the associated argument as unsigned hexadecimal number with lower-case hex-digits (a-f)
%X Same as %x, but with upper-case hex-digits (A-F)
%f Interpret and print the associated argument as floating point number
%e Interpret the associated argument as double, and print it in <N>±e<N> format
%E Same as %e, but with an upper-case E in the printed format
%g Interprets the associated argument as double, but prints it like %f or %e
%G Same as %g, but print it like %E
%c Interprets the associated argument as character: only the first character of a given argument is printed
%s Interprets the associated argument literally as string
%n No conversion or printing is done. Assigns the number of so far printed characters to the variable named in the corresponding argument (similat to C's printf)
%(FORMAT)T output the date-time string resulting from using FORMAT as a format string for strftime(3). The associated argument is the number of seconds since Epoch, or -1 (current time) or -2 (shell startup time)
%% No conversion is done. Produces a % (percent sign)

Some of the mentioned format specifiers can modify their behaviour by getting a format modifier:

Modifiers

To be more flexible in the output of numbers and strings, the printf command allows format modifiers. These are specified between the introductory % and the character that specifies the format:

printf "%50s\n" "This field is 50 characters wide..."

Field and printing modifiers

Field output format
<N>Any number: Specifies a minimum field width, if the text to print is shorter, it's padded with spaces, if the text is longer, the field is expanded
.The dot: Together with a field width, the field is not expanded when the text is longer, the text is truncated instead. "%.s" is an undocumented equivalent for "%.0s", which will force a field width of zero, effectively hiding the field from output
*The asterisk: the width is given as argument before the string or number. Usage (the "*" corresponds to the "20"): printf "%*s\n" 20 "test string"
#"Alternative format" for numbers: see table below
-Left-bound text printing in the field (standard is right-bound)
0Pads numbers with zeros, not spaces
<space>Pad a positive number with a space, where a minus (-) is for negative numbers
+Prints all numbers signed (+ for positive, - for negative)

The "alternative format" modifier #:

Alternative Format
%#oThe octal number is printed with a leading zero, unless it's zero itself
%#x, %#XThe hex number is printed with a leading "0x"/"0X", unless it's zero
%#g, %#GThe float number is printed with trailing zeros until the number of digits for the current precision is reached (usually trailing zeros are not printed)
all number formats except %d, %o, %x, %XAlways print a decimal point in the output, even if no digits follow it

Precision

The precision for a floating- or double-number can be specified by using .<DIGITS>, where <DIGITS> is the number of digits for precision. If <DIGITS> is an asterisk (*), the precision is read from the argument that precedes the number to print, like (prints 4,3000000000):

printf "%.*f\n" 10 4,3
The format .*N to specify the N'th argument for precision does not work in Bash.

For strings, the precision specifies the maximum number of characters to print (i.e., the maximum field width). For integers, it specifies the number of digits to print (zero-padding!).

Escape codes

CodeDescription
\\Prints the character \ (backslash)
\aPrints the alert character (ASCII code 7 decimal)
\bPrints a backspace
\fPrints a form-feed
\nPrints a newline
\rPrints a carriage-return
\tPrints a horizontal tabulator
\vPrints a vertical tabulator
\"Prints a '
\?Prints a ?
\<NNN>Interprets <NNN> as octal number and prints the corresponding character from the character set
\0<NNN>same as \<NNN>
\x<NNN>Interprets <NNN> as hexadecimal number and prints the corresponding character from the character set (3 digits)
\u<NNNN>same as \x<NNN>, but 4 digits
\U<NNNNNNNN>same as \x<NNN>, but 8 digits

Examples

Snipplets

  • print the decimal representation of a hexadecimal number (preserve the sign)
    • printf "%d\n" 0x41
    • printf "%d\n" -0x41
    • printf "%+d\n" 0x41
  • print the octal representation of a decimal number
    • printf "%o\n" 65
    • printf "%05o\n" 65 (5 characters width, padded with zeros)
  • this prints a 0, since no argument is specified
    • printf "%d\n"
  • print the code number of the character A
    • printf "%d\n" \'A
    • printf "%d\n" "'A"
  • Generate a greeting banner and assign it to the variable GREETER
    • printf -v GREETER "Hello %s" "$LOGNAME"
  • Print a text at the end of the line, using tput to get the current line width
    • printf "%*s\n" $(tput cols) "Hello world!"

Small code table

This small loop prints all numbers from 0 to 127 in

  • decimal
  • octal
  • hex

for ((x=0; x <= 127; x++)); do
  printf '%3d | %04o | 0x%02x\n' "$x" "$x" "$x"
done

Ensure well-formatted MAC address

This code here will take a common MAC address and rewrite it into a well-known format (regarding leading zeros or upper/lowercase of the hex digits, …):

the_mac="0:13:ce:7:7a:ad"

# lowercase hex digits
the_mac="$(printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${the_mac//:/ 0x})"

# or the uppercase-digits variant
the_mac="$(printf "%02X:%02X:%02X:%02X:%02X:%02X" 0x${the_mac//:/ 0x})"

Replacement echo

This code was found in Solaris manpage for echo(1).

Solaris version of /usr/bin/echo is equivalent to:

printf "%b\n" "$*"

Solaris /usr/ucb/echo is equivalent to:

if [ "X$1" = "X-n" ]
then
     shift
     printf "%s" "$*"
else
     printf "%s\n" "$*"
fi

prargs Implementation

Working off the replacement echo, here is a terse implementation of prargs:

printf '"%b"\n' "$0" "$@" | nl -v0 -s": "

repeating a character (for example to print a line)

A small trick: Combining printf and parameter expansion to draw a line

length=40
printf -v line '%*s' "$length"
echo ${line// /-}
or:
length=40
eval printf -v line '%.0s-' {1..$length}

Replacement for some calls to date(1)

The %(…)T format string is a direct interface to strftime(3).

$ printf 'This is week %(%U/%Y)T.\n' -1
This is week 52/2010.

Please read the manpage of strftime(3) to get more information about the supported formats.

Using printf inside of awk

Here's the gotcha:

$ printf "%s\n" "Foo"
Foo

$ echo "Foo" | awk '{ printf "%s\n" $1 }'
awk: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
	`%s
Foo'
	 ^ ran out for this one

One fix is to use commas to separate the format from the arguments:

$ echo "Foo" | awk '{ printf "%s\n", $1 }'
Foo

Or, use printf the way that awk wants you to:

$ echo "Foo" | awk '{ printf $1 "\n" }'
Foo

But then you lose the ability to pad numbers, set field widths, etc. that printf has.

See also

Discussion

Dan Douglas, 2011/09/10 05:36

A somewhat glaring omission from every shell I have to test with other than ksh93 is the numbered argument conversion specifiers. %n$ or *n$ http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html

The idea is to allow either rearranging the order of the arguments, or reusing a width modifier (with *), by addressing which conversions apply to which args. Unfortunately these don't really save you any typing because numbering any of the conversions causes the behavior when there are more arguments than conversions to go away. The spec says behavior is unspecified in this case. Ksh simply segfaults (as the 3rd example below). The first and second are essentially equivalent.

~ $ printf '%.*s\n' 3 'foobar' 3 'foobarbaz' #Bash
foo
foo
~ $ ksh
$ printf '%.*1$s\n%3$.*1$s\n' 3 'foobar' 'foobarbaz'
foo
foo
$ printf '%.*1$s\n' 3 'foobar' 'foobarbaz'          
Segmentation fault
~ $ printf '%.*1$s\n%3$.*1$s\n' 3 'foobar' 'foobarbaz' #Back in Bash
-bash: printf: `1': invalid format character
~ $

Kind of dumb the way it's specified but I can imagine a few scenarios where it might be useful. And given that Bash simply points to printf(3) as documentation it's a rather odd thing to miss even though it's also missing in almost every other shell's builtins plus printf(1) of gnu coreutils. (It's described in printf(3) of the linux-manpages too, which mentions it's SUS, but not C99.)

Altair IV, 2012/05/18 16:19

Umm, what? That last section has no business being here. awk's built-in printf function is a completely different entity from the shell's version. The syntax for awk's printf is comma-delimited, i.e. 'printf( "<format>" , "<arguments>" )' , with the parentheses being optional. So it's actually the first "fix" that's using it correctly.

Dan Douglas, 2012/07/12 08:13

So fix it, this is a wiki. :)

MJF, 2012/07/12 14:18
That last section has no business being here. awk's built-in printf function is a completely different entity from the shell's version.

Yeah, it's bloody obvious that 'printf' and 'printf' are "completely different" things. The fact that the syntax for the two is only slightly different is just a massive coincidence, and can't possibly lead to any confusion. And why bother to document that difference? People should just read the source code to figure out why awk is puking out errors, right?

R.W. Emerson II, 2012/12/08 19:07

I've found that the following lines produce different results:

  var=$(printf ...)  
  printf -v var ...

For example, the first line below omits the trailing \n, but the second line retains it:

declare vT ; vT=$(printf "%s\n" "ABC") ; echo "vT($vT)"
declare vT ; printf -v vT "%s\n" "ABC" ; echo "vT($vT)"

It's nice to finally find a site that documents these bash commands in depth. I've been searching for hours, in vain, for an article or post that mentions the above printf bug/feature. I'm surprised to find that no one else has mentioned it.

Jan Schampera, 2012/12/16 14:19

The reason is that a command substitution $() cuts a trailing newline, as mentioned in the article about command substitution.

Thus, your notice is absolutely correct. These two commands produce slightly different results and I should mention it above.

Enter your comment. Wiki syntax is allowed:
 
commands/builtin/printf.1330301388.txt · Last modified: 2012/02/27 01:09 by pi3832
GNU Free Documentation License 1.3
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0