echo指令用于字符串的输出,可以使用echo进行复杂的输出格式控制:
1.普通字符串的输出
echo "It is a test"#双引号完全可以省略
2.显示转义字符
echo "\"It is a test\""
3.显示变量
#!/bin/bash
read name
echo "$name is a test"
以上代码保存为 test.sh,name 接收标准输入的变量,结果将是:
[root@www ~]# sh test.sh
OK #标准输入
OK It is a test #输出
4.显示换行和显示不换行
echo -e "ok! \n"#-e开启转义
echo -e "ok! \c"#-e开启转义 \c 不换
5.显示结果定向至文件
echo "hello" > myfile
6.原样输出字符串,不进行转义或取变量—用单引号
echo '$name\"'
8.显示命令执行结果
echo `date`
注意:这里使用反引号而不是单引号。结果将显示日期。