Shell printf command
In the previous section we learned about the echo command of the shell, and in this section we learned about printf, another output command of the shell.
The printf command mimics the printf() program in the C library.
Standards are defined, so scripts that use printf are better than scripts that use echo portability.
Printf uses parameters separated by reference text or spaces, formatted strings can be used outside of printf, and the width of strings, left-right alignment, and so on can be made. The default printf doesn't automatically add line breaks like echo, we can add them manually.
The syntax of the printf command:
printf format-string [arguments...]
Description of the parameters:
- format-string: Controls strings for formatting
- arguments: is a list of parameters.
Here's an example:
$ echo "Hello, Shell" Hello, Shell $ printf "Hello, Shell\n" Hello, Shell $
Next, let me use a script to embody the power of printf:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876
Execute the script and the output looks like this:
姓名 性别 体重kg 郭靖 男 66.12 杨过 男 48.65 郭芙 女 47.99
%s %c %d %f are format substitutions
%-10s refers to a width of 10 characters (-for left alignment, no right alignment), any character is displayed within a 10-character wide character, and if not enough, it is automatically filled with spaces, and more than that will be displayed in full.
%-4.2f means formatted as a number of places, where .2 refers to the retention of 2 bits of the scale.
More examples:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn # format-string为双引号 printf "%d %s\n" 1 "abc" # 单引号与双引号效果一样 printf '%d %s\n' 1 "abc" # 没有引号也可以输出 printf %s abcdef # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用 printf %s abc def printf "%s\n" abc def printf "%s %s %s\n" a b c d e f g h i j # 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替 printf "%s and %d \n"
Execute the script and the output looks like this:
1 abc 1 abc abcdefabcdefabc def a b c d e f g h i j and 0
The escape sequence of printf
Sequence | Description |
---|---|
\a | The warning character, usually the BEL character of ASCII |
\b | Back off |
\c | Suppresses (does not display) line break characters at any end in the output (valid only in parameter strings controlled by the %b format indicator), and any characters left in the argument, any subsequent parameters, and any characters left in the format string are ignored |
\f | Change the page (formfeed) |
\n | Line change |
\r | Carriage return |
\t | Horizontal tabs |
\v | Vertical tabs |
\\ | A literal backslash character |
\ddd | A character that represents a 1- to 3-digit octal value. Valid only in format strings |
\0ddd | Represents a 1 to 3-bit octal character |
Instance
$ printf "a string, no processing:<%s>\n" "A\nB" a string, no processing:<A\nB> $ printf "a string, no processing:<%b>\n" "A\nB" a string, no processing:<A B> $ printf "www.w3cschool.cn \a" www.w3cschool.cn $ #不换行