Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [86]

By Root 945 0
varies by format specifier, as shown in Table 7-3.

Table 7-3. 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.

Here are some quick examples of the precision in action:

$ printf "%.5d\n" 15

00015

$ printf "%.10s\n" "a very long string"

a very lon

$ printf "%.2f\n" 123.4567

123.46

The C library printf( ) function allows you to specify the width and precision dynamically, via additional values in the argument list. The POSIX standard doesn't supply this, instead recommending the use of shell variable values in the format string.[3] Here is an example:

$ width=5 prec=6 myvar=42.123456

$ printf "|%${width}.${prec}G|\n" $myvar

POSIX

|42.1235|

$ printf "|%*.*G|\n" 5 6 $myvar

ksh93 and bash

|42.1235|

Finally, one or more flags may precede the field width and the precision. We've already seen the - flag for left justification. The complete set of flags is shown in Table 7-4.

Table 7-4. 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.

And again, here are some quick examples:

$ 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

For the %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.

* * *

[3] Some versions of printf, such as those built into ksh93 and bash, do support dynamic width and precision specifications.

Tilde Expansion and Wildcards

The shell does two different expansions related to filenames. The first is tilde expansion, and the second is variously termed wildcard expansion, globbing, or pathname expansion.

Tilde Expansion

The shell performs tilde expansion if the first character of a command-line string is a tilde (~), or if the first character

Return Main Page Previous Page Next Page

®Online Book Reader