shell echo命令
格式命令:
echo string
1.显示普通字符串:
echo It is a test
或者(忽略双引号):
echo It is a test
2.显示转义字符(双引号也可以省略)
echo " \"It is a test\" ”
结果:"It is a test"
3.显示变量
read命令从标准输入中读取一行并把输入行的每个字段的值指定给shell变量
#test.sh
read name
echo "$name It is a test"
以上代码保存是test.sh. name接受标准输入的变量,输出:
# sh test.sh
ok #标准输出
ok It is a test #输出
注:read命令一个一个词组的接受输入的参数,每个词组需要使用空格进行分隔;
若输入的参数大于需要的参数,则多出的参数将被作为最后一个参数接受。
4.显示换行/不显示换行
echo -e "ok! \n" # -e开启转义, \n 表示换行
echo -e "no! \c" # \c 不换行
echo "It is a test"
结果:
ok!
no!
It is a test
6.显示结果定向至文件
echo "It is a test" > myfile
7.显示命令执行结果
echo `date` # `是反引号
结果:
Thu Jul 24 10:.8:46 CST 2016