The printf command mimics the printf () program in the C library.
Standard, so scripting with printf is better than using echo portability.
printf uses reference text or space-delimited parameters, which can be used outside of the format string in printf, as well as the width of the string, left and right alignment, and so on. Default printf does not automatically add line breaks like echo, we can add \ n manually.
The syntax of the printf command:
printf format-string [arguments ...]
Parameter description:
Examples are as follows:
$ echo "Hello, Shell" Hello, shell$ printf "Hello, shell\n" Hello, shell$
Next, I'll use a script to demonstrate the power of printf:
#!/bin/bash printf "%-10s%-8s%-4s\n" name sex weight kg printf "%-10s%-8s%-4.2f\n" Guo Jing male 66.1234 printf "%-10s%-8s%-4.2f\n" Yang over male 48.6543 printf "%-10s%-8s%-4.2f\n" Guoff female 47.9876
Execute the script with the output as follows:
Name gender weight kg Guo Jingnan 66.12 Yang over male 48.65 Guoff female 47.99
%s%c%d%f are format alternates
%-10s refers to a width of 10 characters (-for left alignment, not to right-aligned), any character will be displayed in 10 character justifies characters, if not enough is automatically filled with spaces, more than will also show the content.
%-4.2F refers to the format of decimals, where. 2 refers to 2 decimal places reserved.
More examples:
#!/bin/bash # format-string for double quotes printf "%d%s\n" 1 "ABC" # single quote with double quote effect printf '%d%s\n ' 1 "abc" # no quotes can also output printf%s abcdef# The format specifies only one parameter, but the extra parameter is still output in that format, format-string is reused for printf%s abc defprintf "%s\n" ABC defprintf "%s%s%s\n" a b c D e F g h i J # If there is no arguments, then%s is replaced with null,%d with 0 instead of printf "%s and%d \ n"
Execute the script with the output as follows:
1 ABC1 Abcabcdefabcdefabcdefa b CD e FG H IJ and 0
Escape sequences for printf
sequence |
Description |
\a |
A warning character, usually an ASCII bel character |
\b |
Back off |
\c |
Suppresses (does not display) the newline character at any end of the output (only valid in the parameter string under the control of the%b format designator), and any characters left in the argument, any subsequent arguments, and any characters left in the format string are ignored |
\f |
Page Change (formfeed) |
\ n |
Line break |
\ r |
Enter (carriage return) |
\ t |
Horizontal tab |
\v |
Vertical tab |
\\ |
A literal backslash character |
\ddd |
A character that represents an octal value of 1 to 3 digits. Valid only in format strings |
\0ddd |
Represents octal value characters from 1 to 3 bits |
Instance
$ printf "A string, no processing:<%s>\n" "A\NB" a string, no processing:<a\nb>$ printf "a string, no Processin g:<%b>\n "" A\nb "A string, no processing:<ab>$ printf" www.runoob.com \a "www.runoob.com $ #不换行
This article is from the "Wind Trace _ Snow Tiger" blog, please be sure to keep this source http://snowtiger.blog.51cto.com/12931578/1941352
shell--6, Shell printf command