echo在linux帮助文档中的描述是显示一行文本,类似于python、java等编程语言中的print语句,但实际上它的作用不止于此。您可以使用manecho查看详细的参数说明。example1:显示一行文字,任何特殊字符都不会转义'hello\nworld[root@aliyun-hk1linux-shell-test]#echohelloworldhelloworld[root@aliyun-hk1linux-shell-test]#example2:显示一行文字,末尾不输出换行符[root@aliyun-hk1linux-shell-test]#echo-nhelloworldhelloworld[root@aliyun-hk1linux-shell-test]#echohelloworldhelloworldexample3:显示一行文字,启用反斜杠后的转义符[root@aliyun-hk1linux-shell-test]#echo-e'hello\nworld'helloworld[root@aliyun-hk1linux-shell-test]#echo-e'hello\tworld'helloworldexample4:显示一行文本,禁用反斜杠下面的转义符,echo默认参数[root@aliyun-hk1linux-shell-test]#echo-E'hello\nworld'hello\nworld[root@aliyun-hk1linux-shell-test]#echo-E'hello\tworld'hello\tworldexample5:echo和cat的区别,echo只用来输出文本,cat用来输出文件内容或者从标准输入输出[root@aliyun-hk1linux-shell-test]#echohellohello[root@aliyun-hk1linux-shell-test]#cathellocat:hello:没有那个文件或目录[root@aliyun-hk1linux-shell-test]#echo/etc/hostname/etc/hostname[root@aliyun-hk1linux-shell-test]#cat/etc/hostnamealiyun-hk1[root@aliyun-hk1linux-shell-test]#echohello|cathello[root@aliyun-hk1linux-shell-test]#examle6:echo在自动化构建中的作用,比如我们可以将DB中返回的数据格式化成ansible需要的数据,传入某行通过with_lines在某些情况下,从网络、DB等获取的标准输出,可以通过echo结合awk和grep进行格式化或数据清洗,然后在后续的脚本中使用。[root@aliyun-hk1linux-shell-test]#echo-en'namephoneaddr\nrobin13712345678CN\ntom13812345678HK\n'namephoneaddrrobin13712345678CNtom13812345678HK[root@aliyun-hk1linux-shell-test]#echo-en'namephoneaddr\nrobin13712345678CN\ntom13812345678HK\n'|awk'NR>1{print$1}'robintom-name:showtheitemsfromDBdebug:msg:"{{item}}"with_lines:"echo-en'namephoneaddr\nrobin13712345678CN\ntom13812345678HK\n'|awk'NR>1{print$1}'TASK[显示数据库中的项目]***************************************************************************************************************************************************************************************************************************************************确定:[localhost]=>(item=robin)=>{"msg":"robin"}确定:[localhost]=>(item=tom)=>{"msg":"tom"}example7:echook将获取到的格式化数据写入文件,等待后续使用.[root@aliyun-hk1ansible-test]#echo-en'namephoneaddr\nrobin13712345678CN\ntom13812345678HK\n'|awk'NR>1{print$1}'>DataFromDB1.txt[root@aliyun-hk1ansible-test]#catDataFromDB1.txtrobintom[root@aliyun-hk1ansible-test]#
