incomplete
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.
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.
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!).
-v VAR | If 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
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-Format | Description |
|---|---|
N | A normal decimal number |
0N | An octal number |
0xN | A hexadecimal number |
0XN | A 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 less 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.
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
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!
| Format | Description |
|---|---|
%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 |
%b | Interprets the associated argument as a string and interpreting escape sequences in it |
%q | Prints the associated argument in a format, that it can be re-used as shell-input (escaped spaces etc..) |
Some of the mentioned format specifiers can modify their behaviour by getting a format modifier:
To be more flexible in the output of numbers and strings, the printf command allows format modifiers. These are specified between the introducting % and the character that specifies the format:
printf "%50s\n" "This field is 50 characters wide..."
| Field output format | |
|---|---|
<N> | Any number: Specifies a minimum field width, if the text to print is smaller, it's padded with spaces, if the text is bigger, the field is expanded |
. | The dot: Together with a field width, the field is not expanded when the text is bigger, the text is cutted 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. 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) |
0 | Pads 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 | |
|---|---|
%#o | The octal number is printed with a leading zero, unless it's zero itself |
%#x, %#X | The hex number is printed with a leading ”0x”/”0X”, unless it's zero |
%#g, %#G | The 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, %X | Always print a decimal point in the output, even if no digits follow it |
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,3The 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!).
marks a code that may not be present, because it's not standardized by POSIX®.
| Code | Description |
|---|---|
\\ | Prints the character \ (backslash) |
\a | Prints the alert character (ASCII code 7 decimal) |
\b | Prints a backspace |
\f | Prints a form-feed |
\n | Prints a newline |
\r | Prints a carriage-return |
\t | Prints a horizontal tabulator |
\v | Prints a vertical tabulator |
\<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 |
printf ”%d\n” 0×41printf ”%d\n” -0×41printf ”%+d\n” 0×41printf ”%o\n” 65printf ”%05o\n” 65 (5 characters width, padded with zeros)printf ”%d\n”Aprintf ”%d\n” \'Aprintf ”%d\n” ”'A”GREETERprintf -v GREETER “Hello %s” “$LOGNAME”tput to get the current line widthprintf ”%*s\n” $(tput cols) “Hello world!”This small loop prints all numbers from 0 to 127 in
for ((x=0; x <= 127; x++)); do printf '%3d | %04o | 0x%02x\n' "$x" "$x" "$x" done
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})"
incomplete
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
Working off the replacement echo, here is a terse implementation of prargs:
printf '"%b"\n' "$0" "$@" | nl -v0 -s": "
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}