printf
 

Usage

printf format [ string ... ]
 

Purpose

 

Major options

 

Behavior

 

printf escape sequences

Sequence Description
\a Alert character, usually the ASCII BEL character.
\b Backspace.
\c Suppress any final newline in the output.[*] Furthermore, any characters left in the argument, any following arguments, and any characters left in the format string are ignored (not printed).
\f Formfeed.
\n Newline.
\r Carriage return.
\t Horizontal tab.
\v Vertical tab.
\\ A literal backslash character.
\ddd Character represented as a 1- to 3-digit octal value. Valid only in the format string.
\0ddd Character represented as a 1- to 3-digit octal value.
 
Confusion of Printf Escape Sequences
 

printf format specifiers

Item Description
%b The corresponding argument is treated as a string containing escape sequences to be processed.
%c ASCII character. Print the first character of the corresponding argument.
%d, %i Decimal integer.
%e Floating-point format ([-]d.precisione[+-]dd).
%E Floating-point format ([-]d.precisionE[+-]dd).
%f Floating-point format ([-]ddd.precision).
%g %e or %f conversion, whichever is shorter, with trailing zeros removed.
%G %E or %f conversion, whichever is shorter, with trailing zeros removed.
%o Unsigned octal value.
%s String.
%u Unsigned decimal value.
%x Unsigned hexadecimal number. Use a-f for 10 to 15.
%X Unsigned hexadecimal number. Use A-F for 10 to 15.
%% Literal %.
 

Format Expression
 
To specify the width and alignment of output fields.

   Example: right-justify 

$ printf "|%10s|\n" hello |     hello|
Example: right-justify
$ printf "|%-10s|\n" hello |hello     |
 

Precision
 
The precision modifier is optional.

 
Meaning of precision
Conversion Precision means
%d, %i, %o, %u, %x, %X The minimum number of digits to print.
When the value has fewer digits, it is padded with leading zeros.
The default precision is 1.
%e, %E The minimum number of digits to print.
When the value has fewer digits, it is padded with zeros after the decimal point.
The default precision is 6.
A precision of 0 inhibits printing of the decimal point.
%f The number of digits to the right of the decimal point.
%g, %G The maximum number of significant digits.
%s The maximum number of characters to print.
 

Flags for printf

 
Flags for printf
Character Description
- Left-justify the formatted value within the field.
space Prefix positive values with a space and negative values with a minus.
+ Always prefix numeric values with a sign, even if the value is positive.
# Use an alternate form: %o has a preceding 0;
%x and %X are prefixed with 0x and 0X, respectively;
%e, %E, and %f always have a decimal point in the result; and %g and %G do not have trailing zeros removed.
0 Pad output with zeros, not spaces.
This happens only when the field width is wider than the converted result.
In the C language, this flag applies to all output formats, even nonnumeric ones.
For the printf command, it applies only to the numeric formats.

   Example: 

$ printf "|%-10s| |%10s|\n" hello world Left-, right-justified strings |hello | | world|
$ printf "|% d| |% d|\n" 15 -15 #Space flag | 15| |-15|
$ printf "%+d %+d\n" 15 -15 #+ flag +15 -15
$ printf "%x %#x\n" 15 15 '#' flag f 0xf
$ printf "%05d\n" 15 # 0 flag 00015
 
Speical Treatment of %b, %c, and %s conversion specifiers
the corresponding arguments are treated as strings.
Otherwise, they're interpreted as C-language numeric constants
(leading 0 for octal, and leading 0x or 0X for hexadecimal).
Furthermore, if an argument's first character is a single or double quote,
the corresponding numeric value is the ASCII value of the string's second character:
$ printf "%s is %d\n" a "'a"

a is 97
 
When there are more arguments than format specifiers,
the format specifiers are reused as needed. This is convenient when the argument list is of unknown length, such as from a wildcard expression.
 
If there are more specifiers left in the format string than arguments,
the missing values are treated as zero for numeric conversions and as the empty string for string conversions.
(This seems to be only marginally useful. It's much better to make sure that you supply the same number of arguments as the format string expects.)
 
If printf cannot perform a format conversion,
     
    it returns a nonzero exit status.