Shell-05 Shell Echo&Printf
- Echo Command
The echo command in Shell is similar to the echo command in PHP in that it is used for string output.
echo string
1.1 Show Common String
echo "It is a test" # " " ignorable,same as (echo It is a test)
2. Show escaped characters
echo "\"It is a test\"" # "It is a test", " " ignorable, same as (\"It is a test\")
3. Show variables
The read command reads a line from standard input and assigns the value of each field on the line to a shell variable.
#!/bin/sh
read name
echo "$name It is a test"
4. Show line feeds
echo -e "OK! \n" # -e Enable escaping
echo "It is a test"
5. Show no line feeds
#!/bin/sh
echo -e "OK! \c"
echo "It is a test"
6. Show results directed to file
echo "It is a test" > myfile
7. Output the string as is, without escaping or taking variables (in single quotes)
echo '$name\"'
8. Show command execution results
echo `date` // backslash (punct.)
2. Printf Command
The printf command mimics the printf() program in the C library.
Printf is defined by the POSIX standard, so scripts that use printf are more portable than those that use echo.
printf takes quoted text or space-separated arguments, and you can use formatted strings in printf, as well as specify string widths, left/right alignment, and so on. By default, printf does not automatically add line breaks as echo does, so you can add them manually.
The syntax of the printf command:
printf format-string [arguments...]
Parameter Description:
- format-string: format control string
- arguments: the list of parameters.
$ echo "Hello, Shell"
Hello, Shell
$ printf "Hello, Shell\n"
Hello, Shell
#!/bin/bash
printf "%-10s %-8s %-4s\n" Name Gender Weightkg
printf "%-10s %-8s %-4.2f\n" GuoJing Male 66.1234
printf "%-10s %-8s %-4.2f\n" YangGuo Male 48.6543
printf "%-10s %-8s %-4.2f\n" GuoFu Female 47.9876
Execute the script and the output is shown below:
Name Gender Weightkg
GuoJing Male 66.12
YangGuo Male 48.65
GuoFu Female 47.99
| Symbol | Description |
|---|---|
| %s | Output a String |
| %d | Output an integer |
| %c | Output a character |
| %f | Outputs a real number as a decimal |
| %-10s | Width is 10 characters (- means left-justified, otherwise means right-justified), any character will be displayed within 10 characters wide, if less than 10 characters then space will be filled, more than 10 characters will display the whole contents |
| %-4.2f | Decimal format, where .2 means 2 decimal places |
#!/bin/bash
printf "%d %s\n" 1 "abc" // # format-string为双引号
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"
执行脚本,输出结果如下所示:
1 abc
1 abc
abcdefabcdefabc
def
a b c
d e f
g h i
j
and 0
printf 的转义序列
| 序列 | 说明 |
|---|---|
| \a | 警告字符,通常为ASCII的BEL字符 |
| \b | 后退 |
| \c | 抑制(不显示)输出结果中任何结尾的换行字符(只在%b格式指示符控制下的参数字符串中有效),而且,任何留在参数里的字符、任何接下来的参数以及任何留在格式字符串中的字符,都被忽略 |
| \f | 换页(formfeed) |
| \n | 换行 |
| \r | 回车(Carriage return) |
| \t | 水平制表符 |
| \v | 垂直制表符 |
| \ | 一个字面上的反斜杠字符 |
| \ddd | 表示1到3位数八进制值的字符。仅在格式字符串中有效 |
| \0ddd | 表示1到3位的八进制值字符 |
$ 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.runoob.com \a"
www.runoob.com $ #不换行

浙公网安备 33010602011771号