This article is part of the Linux Shell Series Tutorial (eight), more shell tutorials: Linux Shell Tutorials
In previous: Linux Shell series Tutorial (vii) Shell output This article has a brief introduction to the Shell printf command, which gives you a detailed introduction to the printf command in the shell.
One, Shell printf command syntax
printf format-string [arguments ...]
Format-string is a string describing the format specification, used to describe how the output is arranged, preferably in quotation marks for this string. This string contains literal characters as well as a format declaration, a special placeholder for the format declaration, to describe how the corresponding parameter is displayed.
Arguments is a list of parameters that correspond to a format declaration, such as a series of string or variable values.
A format declaration consists of two parts: a percent sign (%) and an indicator.
The most commonly used format indicators are two,%s is used for strings, and%d is used for decimal integers.
In the format string, the generic character is literally displayed. The escape sequence is interpreted like echo and then output to the corresponding character. A format declaration begins with a% symbol and ends with one of the defined set of letters to control the output of the corresponding parameter.
Second, Shell printf command escape sequence
The escape sequence for the shell printf command is shown in the following table
Sequence |
|
\a |
A warning character, usually an ASCII bel character |
\b |
Back off |
\c |
The newline character at any end of the output is not displayed, and any characters left in the argument, any subsequent arguments, and any characters left in the format string are ignored. |
\f |
Page change |
\ n |
Line break |
\ r |
Enter |
\ t |
Horizontal tab |
\v |
Vertical tab |
\\ |
Backslash character |
Third, Shell printf command format indicator
The format indicator for the shell printf command is shown in the following table
Symbol |
Description |
%c |
ASCII characters. Displays 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]) |
%g |
%e or%f conversion, see which one is shorter, then delete the end of the 0 |
%G |
%e or%f conversion, see which one is shorter, then delete the end of the 0 |
%s |
String |
%u |
Decimal value with no sign |
%x |
hexadecimal with no sign. Use A to F for 10 to 15 |
%% |
Literal meaning of the% |
%x |
hexadecimal with no sign. Use A to F for 10 to 15 |
Iv. Shell printf Command Precision format indicator
The precision format indicator in the shell printf command is shown in the following table
Symbol |
Meaning |
%d,%i,%o,%u,%x,%x |
The minimum number of digits to print. When the number of digits for a value is less than this number, 0 is prepended. The default precision is 1 |
%e,%e |
The minimum number of digits to print. When the number of digits for a value is less than this number, a value of 0 is appended to the decimal point, and the default is 6. The precision is 0 means the number of digits to the right of the decimal point is not displayed |
%f |
Number of digits to the right of the decimal |
%g,%g |
Maximum number of significant digits |
%s |
The maximum number of characters to print |
Examples of Use:
printf "% .5d \ n" 15
Output: 00015
printf "% .10s \ n" "a very long string"
Output: a very lon
printf "% .2f \ n" 123.4567
Output: 123.46
Five, Shell printf command some identifiers
Some common identifiers for Shell printf commands are shown in the following table
Character |
Description |
– |
Aligns formatted values in a field to the left |
Space |
Place a space before a positive value, and a minus sign before a negative value |
+ |
Always place a plus or minus sign before the value, even if the positive value is |
# |
One of the following forms is selected:%o has a pre-placed O; %x and%x respectively 0x and 0X before; %e,%e and%f always have a decimal points in the result; %g with%g for no end of the 0. |
0 |
Fill the output with 0 instead of blank. This only occurs when the field width is greater than the conversion |
Examples of Use:
$ printf "% -20s% -15s% 10.2f \ n" "Shan" "zhang" 35
Output: Shan zhang 35.00
% -20s indicates a left-aligned, 20-character string format with less than 20 characters, with a corresponding number of space characters on the right.
% -15s represents a left-justified 15-character string format.
% 10.2f represents a right-justified, 10-character floating point number, one of which is a decimal point, and two digits are reserved after the decimal point.
$ printf "|% 10s | \ n" hello
Output: | hello |
% 10s indicates a right-justified string with a width of 10.If it is not a string, the corresponding number of spaces are added to the left.
$ printf "|% -10s | \ n" hello
Output: | hello |
Compare with case two
$ printf "% x% # x \ n" 15 15
Output: f 0xf
If the # flag is used in conjunction with% x,% X, when outputting hexadecimal digits, the prefix 0x or 0X is returned.
The use of flags is mainly to dynamically specify the width and precision.
Vi. Some comprehensive examples of shell printf commands
Here is a summary of some of the more comprehensive examples, hoping to help you learn Shell printf command.
Strings are aligned left to right:
$ printf "|% -10s | |% 10s | \ n" hello world
Output | hello | | world |
Blank sign:
$ printf "|% d | |% d | \ n" 15 -15
Output: | 15 | | -15 |
+ Flag:
$ printf "|% + d | |% + d | \ n" 15 -15
Output: | +15 | | -15 |
#Logo:
$ printf "% x ||% # X \ n" 15 15
Output: f || 0XF
0 flags:
$ printf "% 05d \ n" 15
Output: 00015
For conversion indicators%b,%c, and%s, the corresponding argument should be a string. Otherwise, they will be interpreted as the numeric constants of the C language (0-bit octal at the beginning, and 0x and 0X at the beginning of the 16 binary).
Further, if the first character of the argument is a single or double quotation mark, the corresponding value is the ASCII value of the second character of the string, such as:
Command: printf "%s is%d \ n" A "' a"
Output: A is 97
When the parameter is more than the format indicator, the format indicator is re-used as needed.
This is handy when the argument list length is unknown.
For example, from a wildcard expression, if the remaining in the format string indicates that the transmitting parameter is too long, if it is a numeric conversion, the missing value is considered to be zero.
However, if it is a string conversion, it is treated as an empty string (although this can be used, but the better way should be a one by one correspondence, that is, the number of arguments supplied and the number of format strings is the same).
If printf cannot format the conversion, it returns a non-zero exit state.
Well, for the shell printf Command in the details of the first to go here, printf is a very powerful command, I hope we can practice a lot, to gain proficiency.
For more shell tutorials See: Linux Shell Series Tutorials
This article by Linux technology talent "Daxue" published in: Linux University
This article fixed link: Linux Shell series Tutorial (eight) Shell printf command detailed
Reprint please keep the link and source!
Linux Shell Series Tutorial (eight) Shell printf command details