温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux echo文本处理命令的使用及示例

发布时间:2020-10-20 17:31:44 来源:脚本之家 阅读:151 作者:李欢欢 栏目:服务器

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 "hello\nworld"   # -e 用于启用反斜杠转义,如 \n 会转换成换行
hello
world

[root@localhost ~]$ echo -E "hello\nworld"   # -E 用于禁用反斜杠转义,默认就是禁用
hello\nworld

常用转义符:

[root@localhost ~]$ echo -e "hello \\ world"  # \\ 用于输出反斜杠
hello \ world

[root@localhost ~]$ echo -e "\a"        # \a 用于响铃,发出声音的响铃哦

[root@localhost ~]$ echo -e "hello\bworld"   # \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 "hello\nworld"  # \n 换行符
hello
world

[root@localhost ~]$ echo -e "hello\rworld"  # \r 用于把光标移到行首,相当于把 \r 前面的字符删除,只输出 \r 后面的字符
world

[root@localhost ~]$ echo -e "hello\tworld"  # \t 制表符,相当于键盘上的Tab键
hello  world

[root@localhost ~]$ echo -e "hello\vworld"  # \v 垂直制表符
hello
   world

echo 输出颜色:

语法:echo -e "\033[字体背景颜色;字体颜色m字符串\033[0m"
例子:echo -e "\033[41;36m something here \033[0m"
解释:其中41的位置代表字体背景颜色,36的位置是代表字体颜色

//输出带颜色的字体
echo -e "\033[30m 黑色字 \033[0m"
echo -e "\033[31m 红色字 \033[0m"
echo -e "\033[32m 绿色字 \033[0m"
echo -e "\033[33m 黄色字 \033[0m"
echo -e "\033[34m 蓝色字 \033[0m"
echo -e "\033[35m 紫色字 \033[0m"
echo -e "\033[36m 天蓝字 \033[0m"
echo -e "\033[37m 白色字 \033[0m"
//输出带背景颜色的字体
echo -e "\033[40;37m 黑底白字 \033[0m"
echo -e "\033[41;37m 红底白字 \033[0m"
echo -e "\033[42;37m 绿底白字 \033[0m"
echo -e "\033[43;37m 黄底白字 \033[0m"
echo -e "\033[44;37m 蓝底白字 \033[0m"
echo -e "\033[45;37m 紫底白字 \033[0m"
echo -e "\033[46;37m 天蓝底白字 \033[0m"
echo -e "\033[47;30m 白底黑字 \033[0m"
//其他属性
\33[0m 关闭所有属性
\33[1m 设置高亮度
\33[4m 下划线
\33[5m 闪烁
\33[7m 反显
\33[8m 消隐
\33[30m — \33[37m 设置前景色
\33[40m — \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标

example1: 显示一行文本,任何特殊字符都不会被转义

[root@aliyun-hk1 linux-shell-test]# echo hello\nworld
hellonworld
[root@aliyun-hk1 linux-shell-test]# echo 'hello\nworld'
hello\nworld
[root@aliyun-hk1 linux-shell-test]# echo hello world
hello world
[root@aliyun-hk1 linux-shell-test]#

example2: 显示一行文本,不要输出末尾的换行符

[root@aliyun-hk1 linux-shell-test]# echo -n hello world
hello world[root@aliyun-hk1 linux-shell-test]# echo hello world
hello world

example3: 显示一行文本,启用反斜杠后面的转义字符

[root@aliyun-hk1 linux-shell-test]# echo -e 'hello\nworld'
hello
world
[root@aliyun-hk1 linux-shell-test]# echo -e 'hello\tworld'
hello  world

example4: 显示一行文本,禁用反斜杠后面的转义字符,echo默认参数

[root@aliyun-hk1 linux-shell-test]# echo -E 'hello\nworld'
hello\nworld
[root@aliyun-hk1 linux-shell-test]# echo -E 'hello\tworld'
hello\tworld

example5: echo与cat的差异对比,echo只用于输出文本,cat用于输出文件内容或者从标准输入中输出

[root@aliyun-hk1 linux-shell-test]# echo hello
hello
[root@aliyun-hk1 linux-shell-test]# cat hello
cat: hello: No such file or directory
[root@aliyun-hk1 linux-shell-test]# echo /etc/hostname
/etc/hostname
[root@aliyun-hk1 linux-shell-test]# cat /etc/hostname
aliyun-hk1
[root@aliyun-hk1 linux-shell-test]# echo hello|cat
hello
[root@aliyun-hk1 linux-shell-test]#

examle6: echo在自动化构建中的作用,例如我们可以将DB中返回的数据格式化成ansible需要的数据,通过with_lines 传入某个task并循环使用。在某些情况下,从网络、DB等方式获取的标准输出,可以通过echo结合awk和grep等实现结果的格式化或数据清洗,然后用到后续的​脚本中。

[root@aliyun-hk1 linux-shell-test]# echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'
name phone addr
robin 13712345678 CN
tom 13812345678 HK
[root@aliyun-hk1 linux-shell-test]# echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'|awk 'NR>1 {print $1}'
robin
tom
- name: show the items from DB
   debug:
    msg: "{{ item }}"
   with_lines: "echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'|awk 'NR>1 {print $1}'
​
TASK [show the items from DB] ****************************************************************************************************************************************************************************************************************ok: [localhost] => (item=robin) => {
  "msg": "robin"
}
ok: [localhost] => (item=tom) => {
  "msg": "tom"
}

example7: echo还可以将获取到并格式化好的数据写入到一个文件,等待后续使用​。

[root@aliyun-hk1 ansible-test]# echo -en 'name phone addr\nrobin 13712345678 CN\ntom 13812345678 HK\n'|awk 'NR>1 {print $1}' > DataFromDB1.txt
[root@aliyun-hk1 ansible-test]# cat DataFromDB1.txt
robin
tom
[root@aliyun-hk1 ansible-test]#

到此这篇关于Linux echo文本处理命令的使用及示例的文章就介绍到这了,更多相关Linux echo命令内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI