echo是Shell的一个内部指令,用于在屏幕上打印输出指定的字符串。
命令格式:echo arg
可用echo实现更复杂的输出格式控制。
(1)显示转义字符
eg:
echo "\"It is a test\""
结果:
"It is a test"
双引号也可以省略。
(2)显示变量
eg:
name="OK"
echo "$name It is a test"
结果:
OK It is a test
同样双引号也可以省略。
如果变量与其它字符相连的话,需要使用大括号({ }):
eg:
mouth=9
echo "${mouth}-7-2017"
结果:
[root@h data]# /bin/sh d.sh
9-7-2017
(3)显示换行
eg:
echo -e "OK!\n"
echo "It is a test"
输出:
OK!
It is a test
(4)显示不换行
eg:
echo -e "OK!\c"
echo "It is a test"
输出:
OK!It si a test
(5)显示结果重定向至文件
eg:
[root@h data]# touch a.log
[root@h data]# ls
a.log
>表示原样输出字符串覆盖原有文件内容:
[root@h data]# echo "my name is Lucy" > a.log
[root@h data]# cat a.log
my name is Lucy
[root@h data]# echo "how are you" > a.log
[root@h data]# cat a.log
how are you
>>表示追加不覆盖
[root@h data]# echo "my name is Lucy" >> a.log
[root@h data]# cat a.log
how are you
my name is Lucy