Shell echo命令

半兽人 发表于: 2016-09-17   最后更新时间: 2018-11-15 23:33:48  
{{totalSubscript}} 订阅, 4,310 游览

echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式:

echo arg

您可以使用echo实现更复杂的输出格式控制。

显示转义字符

echo ""It is a test""

结果将是:
"It is a test"

双引号也可以省略。

显示变量

name="OK"
echo "$name It is a test"

结果将是:

OK It is a test

同样双引号也可以省略。

如果变量与其它字符相连的话,需要使用大括号({ }):

mouth=8
echo "${mouth}-1-2009"

结果将是:

8-1-2009

显示换行

echo -e "OK!\n"
echo "It is a test"

输出:
OK!
It is a test

显示不换行

echo -e "OK!\c"
echo "It is a test"

输出:
OK!It si a test

显示结果重定向至文件

echo "It is a test" > myfile

原样输出字符串

若需要原样输出字符串(不进行转义),请使用单引号。例如:
echo '$name"'

显示命令执行结果

echo `date`

结果将显示当前日期

更新于 2018-11-15
在线,1小时前登录

查看shell更多相关的文章或提一个关于shell的问题,也可以与我们一起分享文章