November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

bash printf to variable

If you want to use bash’s printf formatting and store the value into a variable, you can use bash’s print’s -v flag as follows:

calc_elaspsed_time()
{
local SECONDS=$1
((h=SECONDS/3600))
((m=SECONDS%3600/60))
((s=SECONDS%60))
printf -v ELAPSED_TIME “%02d:%02d:%02d” $h $m $s
}

printf Command
The built-in printf (print formatted) command prints a message to the screen. You will use this command a lot in shell scripts. printf is very similar to the C standard I/O printf() function, but they are not identical. In particular, single- and double-quoted strings are treated differently in shell scripts than in C programs.

The first parameter is a format string describing how the items being printed will be represented. For example, the special formatting code “%d” represents an integer number, and the code “%f” represents a floating-point number.

$ printf “%d\n” 5
5
$ printf “%f\n” 5
5.000000
Include a format code for each item you want to print. Each format code is replaced with the appropriate value when printed. Any characters in the format string that are not part of a formatting instruction are treated as printable characters.

$ printf “There are %d customers with purchases over %d.\n” 50 20000
There are 50 customers with purchases over 20000.
printf is sometimes used to redirect a variable or some unchanging input to a command. For example, suppose all you want to do is pipe a variable to a command. Instead of using printf, Bash provides a shortcut <<< redirection operator. <<< redirects a string into a command as if it were piped using printf. The tr command can convert text to uppercase. This example shows an error message being converted to uppercase with both printf and <<<. $ printf “%s\n” “$ERRMSG” | tr [:lower:] [:upper:] WARNING: THE FILES FROM THE OHIO OFFICE HAVEN’T ARRIVED. $ tr [:lower:] [:upper:] <<< “$ERRMSG” WARNING: THE FILES FROM THE OHIO OFFICE HAVEN’T ARRIVED. The format codes include the following. %a—Represent a floating-point number in hexadecimal format, using lowercase letters %A—Represent a floating point number in hexadecimal format, using uppercase letters %b—Expand backslash sequences %c—Represent a single character %d—Display a signed number %e—Display a floating-point number, shown in exponential (also called “scientific”) notation %f (or %F)—Display a floating-point number without exponential notation %g—(General) Let Bash choose %e or %f, depending on the value %i—Same as %d %0—Display an octal number %q—Quote a string so it can be read properly by a shell script 30 Chapter 3 Files, Users, and Shell Customization %s—Display an unquoted string %u—Display an unsigned number %x—Display an unsigned hexadecimal number, using lowercase letters %X—Display an unsigned hexadecimal number, using uppercase letters %%—Display a percent sign If a number is too large, Bash reports an out-of-range error. $ printf “%d\n” 123456789123456789012 bash: printf: warning: 123456789123456789012: Numerical result out of range For compatibility with C’s printf, Bash also recognizes the following flags, but treats them the same as %d: %j—A C intmax_t or uintmax_t integer %t—A C ptrdiff_t integer %z—A C size_t or ssize_t integer Also for C compatibility, you can preface the format codes with a l or L to indicate a long number. The %q format is important in shell script programming and it is discussed in the quoting section, in the Chapter 5,“Variables.” To create reports with neat columns, numbers can proceed many of the formatting codes to indicate the width of a column. For example, “%10d” prints a signed number in a column 10 characters wide. $ printf “%10d\n” 11 11 Likewise, a negative number left-justifies the columns. $ printf “%-10d %-10d\n” 11 12 11 12 A number with a decimal point represents a column width and a minimum number of digits (or decimal places with floating-point values). For example, “%10.5f” indicates a floating-point number in a 10-character column with a minimum of five decimal places. $ printf “%10.5f\n” 17.2 17.20000 Finally, an apostrophe (‘)displays the number with thousands groupings based on the current country locale. The \n in the format string is an example of a backslash code for representing unprintable characters. \n indicates a new line should be started. There are special backslash formatting codes for the representation of unprintable characters. \b—Backspace \f—Form feed (that is, eject a page on a printer) \n—Start a new line \r—Carriage return \t—Tab \v—Vertical tab \’—Single quote character (for compatibility with C) \\—Backslash n—n is an octal number representing an 8-bit ASCII character $ printf “Two separate\nlines\n” Two separate Lines Any 8-bit byte or ASCII character can be represented by or \ and its octal value. $ printf “ASCII 65 (octal 101) is the character 101\n” ASCII 65 (octal 101) is the character A printf recognizes numbers beginning with a zero as octal notation, and numbers beginning with 0x as hexadecimal notation. As a result, printf can convert numbers between these different notations. $ printf “%d\n” 010 8 $ printf “%d\n “ 0xF 15 $ printf “0x%X\n “ 15 0xF $ printf “0%o\n “ 8 010

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>