shell echo命令
shell的echo指令与PHP的echo指令类似,都是用于字符串的输出。
echo string
1、显示普通字符串
echo "It is a test"
这里的双引号可以省略
2、显示转义字符
echo "\"It is a test\""
3、显示变量
#!/bin/bash
read name
echo "$name It is a test"
name接收标准输入的变量,结果为:
[root@www ~]# sh test.sh
OK #标准输入
OK It is a test #输出
4、显示换行
echo -e "OK!\n" # -e 开启转义
echo "It it a test"
输出结果:
OK!
It it a test
5、显示不换行
#!/bin/sh
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"
#输出结果:
OK! It is a test
6、显示结果定向至文件
echo "It is a test" > myfile
7、原样输出字符串,不进行转义获取变量(用单引号)
echo '$name\"'
输出结果:
$name\"
8、显示执行命令结果
echo `date`
注意:这里使用的是反引号`,而不是单引号'。
结果将显示当前日期
2020年 11月 07日 星期六 15:11:05 CST