shell 的echo指令与PHP的echo指令类似,都是用于字符串的输出。命令格式为:
echo string
显示普通字符串
echo "it is a string"
echo it is a string #双引号可以忽略,输出结果一致
显示转义字符
echo "\"it is a string\""
输出结果为:
"it is a string"
同样,双引号可以忽略
显示变量
#!/bin/bash
read name
echo "$name it si a test"
将以上代码保存为test.sh,name接受标准输入变量,结果将是:
sh test.sh
ok #输入
ok it is a test #输出
显示换行
echo -e "ok! \n" #-e 开启转义
echo "it is a test"
输出结果:
ok!
it is a test
显示不换行
#!/bin/bash
echo -e "ok \c" #开启转义 \c不换行
echo "hello"
输出结果:
ok hello
显示结果定向至文件
echo "It is a test" > myfile
原样输出字符串,不进行转义或取变量(用单引号)
echo '$name\"'
输出结果:
$name\"
显示命令执行结果
echo `date` #注意是反引号,不是单引号
结果将显示当前日期:
Thu Jul 24 10:08:46 CST 2014