Bash printf syntax basics with example - AkuCode

        The format specifiers are letters preceded by a percent sign. Optional modifiers may be placed between the two characters. The specifiers are replaced by the corresponding argument. When there are more arguments than specifiers, the format string is reused until all the arguments have been consumed. The most commonly used specifiers are %s,%d,%f and %x.
        The %s specifier prints the literal characters in the argument:
$ printf “%s\n” Print arguments on “separate lines” 
Print
Arguments
On
Separate lines
%b is like %s except that escape sequences in the arguments are translated:
$ printf “%b\n” “Hello\nworld” “12\tword” 
Hello
World
12 word
        Integers are printed with %d. The integer may be specified as a decimal, octal (using a leading 0), or hexadecimal (preceding the hex number with 0x) number. If the number is not a valid integer, printf prints an error message:
$ printf “%d\n” 23 45 56.78 0xff 011 
23
45
Bash: printf: 56.78: invalid number
0
255
9
        For decimal fractions or floating-point number,use $f. by default they will be printed with six decimal places:
$ printf “%e\n” 12.34 23 56.78 123.45678 
1.234000e+01 
2.300000e+01 
5.678900e+01 
1.234568e+02
        Integers can be printed in hexadecimal using %x for lowercase letters or %X for uppercase letters. For example, when specifying colors for a web page, they are specified in hex notation. I know from the rgb.txt file included with the X window system that the red-green-blue values for royal blue are 65,105, and 225. To convert them to a style rule for a web page, use this:
$ printf “color: #%02x%02x%02;x;\n" 65 105 225 
Color: #4169e1;

Width Specification

        You can modify the formats by following the percent sign with a width specification. The argument will be printed flush right in a field of that width or will be flush left it the number is negative. Here we have the first field with a width of eight characters; the words will be printed flush right. Then there is a field 15 character wide, which will be printed flush left:
$ printf "%8s %-15s:\n" first second third fourth fifth sixth 
 first second :
 third fourth :
 fifth sixth :
        if the width specifications is preceded by a 0, the numbers are padded with leading zeroes to fill the width:
$ printf “%04d\n” 12 23 56 123 255 
0012
0023
0056
0123
0255
        A width specifier with a decimal fraction specifies the precision of a floating-point number or the maximum width of a string:
$ printf “%12.4s %9.2f\n” John 2 Jackson 4.579 Walter 2.9 
John 2.00
Jack 4.58
Walt 2.90
        The script shown in listing 2-2 uses printf to output a simple sales report.
#!/bin/bash 
#: Description : print formatted sales report
## Build a long string of equals signs
divider ==============================================================
divider=$divider$divider
## Format strings for printf
header="\n %-10s %11s %8s %10s\n"
format=" %-10s %11.2f %8d %10.2f\n"
## Width of divider
totalwidth=44
## Print categories
printf "$header" ITEM "PER UNIT" NUM TOTAL
## Print divider to match width of report
printf "%$totalwidth.${totalwidth}s\n" "$divider"
## Print lines of report
printf "$format" \
Chair 79.95 4 319.8 \
Table 209.99 1 209.99 \
Armchair 315.49 2 630.98
## The resulting report looks like this:
ITEM PER UNIT NUM TOTAL
==============================================================
Chair 79.95 4 319.80
Table 209.99 1 209.99
Armchair 315.49 2 630.98
        Note the use of braces around the second total width variable name: ${totalwidth}. In the first instance, the name is followed by a period, which cannot be part of a variable name. in the second, it is followed by the letters, which could be, so the total width name must be separated from it by using braces.

Printing to a Variable

        With version 3.1 bash added a -v option to store the output in a variable instead of printing it to the standard output:
$ printf -v num4 "%04d" 4
$ printf "%s\n" "$num4"
0004

Line Continuation

        At the end of the report script, the last four lines are read as a single line. Using line continuation. A backslash at the end of a line tells the shell to ignore the newline character, effectively joining the next line to the current one.
Comments