当前位置:   article > 正文

Linux Shell - echo 命令输出格式_linux shell echo

linux shell echo

1. echo - display a line of text

Echo the STRING(s) to standard output.

echo [ˈekəʊ]:n. 回响,回声,回音,映现 v. 回响,回荡,发出回声,产生回响
  • 1

1.1. User Commands

(base) yongqiang@yongqiang:~$ man echo
  • 1

1.2. 内容输出后换行 (自带换行符)

输出内容,然后输出换行符号。

(base) yongqiang@yongqiang:~$ echo "yongqiang"
yongqiang
(base) yongqiang@yongqiang:~$ echo yongqiang
yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo "Yongqiang Cheng"
Yongqiang Cheng
(base) yongqiang@yongqiang:~$ echo Yongqiang Cheng
Yongqiang Cheng
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.3. -n - do not output the trailing newline (取消换行符)

取消输出后行末的换行符号,内容输出后不换行。

trail [treɪl]:v. 跟踪,追踪,拖,磨蹭 n. 小径,路径,踪迹,足迹
  • 1
(base) yongqiang@yongqiang:~$ echo "Yongqiang Cheng"
Yongqiang Cheng
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -n "Yongqiang Cheng"
Yongqiang Cheng(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果加入 -n 选项,则在输出内容结束后,不会换行,直接显示新行的提示符。

(base) yongqiang@yongqiang:~$ echo Yongqiang Cheng
Yongqiang Cheng
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo Yongqiang; echo Cheng
Yongqiang
Cheng
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -n Yongqiang; echo Cheng
YongqiangCheng
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

加分号表示一条语句的完结。

1.4. -e - enable interpretation of backslash escapes (支持反斜线控制的字符转换)

检索语句中的转义符并执行,需要使用双引号 " "

If -e is in effect, the following sequences are recognized:

\\     backslash - 输出 \
\a     alert (BEL) - 输出警告音
\b     backspace - 退格键,删除左边一个字符
\c     produce no further output - 取消输出行末的换行符,和 -n 选项一致
\e     escape - Esc 键
\f     form feed - 换页符
\n     new line - 换行符
\r     carriage return - 回车键
\t     horizontal tab - 水平制表符
\v     vertical tab - 垂直制表符
\0NNN  byte with octal value NNN (1 to 3 digits) - 按照八进制 ASCII 码表输出字符,其中 0 为数字 0,NNN 是三位八进制数
\xHH   byte with hexadecimal value HH (1 to 2 digits) - 按照十六进制 ASCII 码表输出字符,其中 hh 是两位十六进制数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
carriage [ˈkærɪdʒ]:n. 运输,运费,举止,仪态
  • 1
(base) yongqiang@yongqiang:~$ echo -e "\\"
\
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\a"

(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

\b 删除左边一个字符。

(base) yongqiang@yongqiang:~$ echo -e "yong\bqiang"
yonqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "yongqiang\b"
yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出内容结束后,不会换行,且 \c 之后的内容不输出。

(base) yongqiang@yongqiang:~$ echo -e "yongqiang"
yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "yongqiang\c"
yongqiang(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "yong\cqiang"
yong(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

\t 水平制表符,Tab 键。

(base) yongqiang@yongqiang:~$ echo -e "Aaa\tBbb\tCcc"
Aaa     Bbb     Ccc
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "Aaa\tBbb\tCcc\nDdd\tEee\tFff"
Aaa     Bbb     Ccc
Ddd     Eee     Fff
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

输入八进制,打印对应的 ASCII 码。65 = \0101 = ‘A’
输入十六进制,打印对应的 ASCII 码。65 = \x41 = ‘A’

(base) yongqiang@yongqiang:~$ echo -e "\0101\t\0102\t\0103\n\0104\t\0105\t\0106"
A       B       C
D       E       F
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\x41\t\x42\t\x43\n\x44\t\x45\t\x46"
A       B       C
D       E       F
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

\n 换行符

(base) yongqiang@yongqiang:~$ echo -e "yong\nqiang"
yong
qiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "yongqiang\n"
yongqiang

(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

\f 换页符

(base) yongqiang@yongqiang:~$ echo -e "yong\fqiang"
yong
qiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "yongqiang\f"
yongqiang

(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

\r 光标回到行首,后面输出的内容会覆盖之前的输出,可用于制作进度条。

(base) yongqiang@yongqiang:~$ echo -e "yong\rqiang"
qiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "yongqiang\rcheng"
chengiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

\v 垂直制表符

(base) yongqiang@yongqiang:~$ echo -e "yong\vqiang\vcheng"
yong
    qiang
         cheng
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5

1.5. 设置打印字体颜色

\e[ 是标准格式,代表颜色输出开始。
\e[0m 是标准格式,代表颜色输出结束。

字体颜色:30m=黑色,31m=红色,32m=绿色,33m=黄色,34m=蓝色,35m=洋红,36m=青色,37m=白色。

(base) yongqiang@yongqiang:~$ echo -e "\e[30m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[31m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[32m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[33m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[34m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[35m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[36m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[37m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

字体颜色:1;30m=亮黑色,1;31m=亮红色,1;32m=亮绿色,1;33m=亮黄色,1;34m=亮蓝色,1;35m=亮洋红,1;36m=亮青色,1;37m=亮白色。

(base) yongqiang@yongqiang:~$ echo -e "\e[1;30m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;31m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;32m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;33m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;34m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;35m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;36m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;37m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

1.6. 设置打印字体背景颜色

\e[ 是标准格式,代表颜色输出开始。
\e[0m 是标准格式,代表颜色输出结束。
背景颜色:40m=黑色,41m=红色,42m=绿色,43m=黄色,44m=蓝色,45m=洋红,46m=青色,47m=白色。

(base) yongqiang@yongqiang:~$ echo -e "\e[40m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[41m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[42m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[43m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[44m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[45m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[46m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[47m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

(base) yongqiang@yongqiang:~$ echo -e "\e[1;40m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;41m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;42m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;43m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;44m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;45m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;46m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\e[1;47m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

1.7. 打印设置

\033\e 效果相同。

(base) yongqiang@yongqiang:~$ echo -e "\e[4m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\033[4m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -e "\033[4m yongqiang \033[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

闪烁显示。

(base) yongqiang@yongqiang:~$ echo -e "\033[5m yongqiang \033[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3

红色字体,白色背景显示。

(base) yongqiang@yongqiang:~$ echo -e "\e[31;47m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3

在这里插入图片描述

1.8. -E - disable interpretation of backslash escapes (default)

禁用转义字符。

(base) yongqiang@yongqiang:~$ echo -e "\e[31;47m yongqiang \e[0m"
 yongqiang
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo -E "\e[31;47m yongqiang \e[0m"
\e[31;47m yongqiang \e[0m
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.9. echo yongqiang cheng

(base) yongqiang@yongqiang:~$ echo yongqiang cheng
yongqiang cheng
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo yongqiang    cheng
yongqiang cheng
(base) yongqiang@yongqiang:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/631198
推荐阅读
相关标签
  

闽ICP备14008679号