Shell 的 echo 命令与 PHP 的 echo 指令类似,都是用于字符串的输出。
命令格式:
echo string
可以使用echo实现更复杂的输出格式控制。
显示普通字符串:
[root@lzx ~]# echo "this is a test."
this is a test.
[root@lzx ~]# echo this is a test. //双引号可以省略
this is a test.
显示转义字符
[root@lzx ~]# echo "\"this is a test.\""
"this is a test."
显示变量
read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量,达成交互效果。
[root@lzx ~]# vim test.sh
#! /bin/bash
read name
echo "$name it's a test."
[root@lzx ~]# sh test.sh
OK //输入OK
OK it's a test. //输出
显示换行
[root@lzx ~]# echo -e "OK \n";echo "It's a test" //-e开启换行,\n 换行
OK
It's a test
[root@lzx ~]# echo -n 111 //-n 不换行
111[root@lzx ~]#
显示不换行
[root@lzx ~]# echo -e "OK \c";echo "It's a test" //-e开启换行,\c 不换行
OK It's a test
显示结果定向至文件
[root@lzx ~]# echo "OK" > 1.txt
[root@lzx ~]# cat 1.txt
OK
原样输出字符串,不进行转义或取变量
[root@lzx ~]# echo '$name\"' //用单引号,原样输出
$name\"
显示命令执行效果
[root@lzx ~]# echo `date` //显示反引号内的命令执行结果
2018年 07月 20日 星期五 17:07:52 CST
[root@lzx ~]# echo `cat 1.txt`
OK