Shell echo命令
Shell 的 echo 指令 与 PHP的 echo 指令类似都是用于字符串的输出。命令格式:
echo string
我们可以使用echo实现更复杂的输出格式控制。
1.显示普通的字符串
echo "This is a String"
这里的双引号完全可以省略,以下命令与上面实例效果一致
echo This is a String
2.显示转义字符
echo "\"This is a String\""
执行脚本,运行结果如下:
"This is a String"
同样,最外面的双引号也可以省略
3.显示变量
read命令从标准输入中读取一行,并把输入行的每个字段的值指定给shell变量
read name echo "$name is the string you just input"
以上代码name接受标准输入的变量,运行结果为:
[root@localhost shell]# ./echo.sh
hehe #标准输入 hehe is the string you just input #输出结果
4.显示换行
echo -e "OK! \n" # -e 开启转义 echo "This is a test"
输出结果:
OK!
This is a test
5.显示不换行
echo -e "Hello! \c" echo "Nice to meet you."
输出结果:
Hello! Nice to meet you.
上面代码echo后不加 -e 命令的话 \c会被作为普通字符串输出
6.显示结果定向至文件
echo "Where should this print?" > myfile
上面代码会将该字符串写到myfile(包含路径,默认当前)文件中,如果该文件不存在则创建该文件
7.原样输出字符串,不进行转义或取变量(用单引号)
echo '$name\"'
输出结果:
$name\"
8.显示命令执行结果(用反引号`)
echo `date`
输出结果为:
2016年 07月 29日 星期五 10:13:59 CST

浙公网安备 33010602011771号