echo 在linux帮助文档的描述是显示一行文本,类似于python和java等编程语言中的print语句,实际上它的作用不仅仅如此。可以使用man echo查看详细的参数说明。
echo命令用于输出指定的字符串,常见用法如下:
[root@localhost ~]$ echo # 输出一个空白行 [root@localhost ~]$ echo "hello world" # 输出指定的字符串 [root@localhost ~]$ echo $HOSTNAME # 输出变量名对应的值 [root@localhost ~]$ echo "hello world" > 1.txt # 输出字符串到指定文件 [root@localhost ~]$ echo `date` # 输出命令的执行结果
常用参数:
[root@localhost ~]$ echo -n "hello world" # -n 不在末尾输出换行符,默认会在末尾输出换行符 hello world[root@localhost ~]$ [root@localhost ~]$ echo -e "hellonworld" # -e 用于启用反斜杠转义,如 n 会转换成换行 hello world [root@localhost ~]$ echo -E "hellonworld" # -E 用于禁用反斜杠转义,默认就是禁用 hellonworld
常用转义符:
[root@localhost ~]$ echo -e "hello world" # 用于输出反斜杠 hello world [root@localhost ~]$ echo -e "a" # a 用于响铃,发出声音的响铃哦 [root@localhost ~]$ echo -e "hellobworld" # b 用于退格,参考:https://blog.csdn.net/lucosax/article/details/34963593 hellworld [root@localhost ~]$ echo -e "hello c world" # c 使用该转义符后,c 后面的字符不再输出 hello [root@localhost ~]$ echo -e "e[32;1m hello world e[35;1m" # e 用于控制字体和背景颜色 hello world [root@localhost ~]$ echo -e "hello f hello f hello" # f 换行,且光标停在换行后原来的地方 hello hello hello [root@localhost ~]$ echo -e "hellonworld" # n 换行符 hello world [root@localhost ~]$ echo -e "hellorworld" # r 用于把光标移到行首,相当于把 r 前面的字符删除,只输出 r 后面的字符 world [root@localhost ~]$ echo -e "hellotworld" # t 制表符,相当于键盘上的Tab键 hello world [root@localhost ~]$ echo -e "hellovworld" # v 垂直制表符 hello world
echo 输出颜色:
语法:echo -e “
