Shell 的 echo 指令與 PHP 的 echo 指令類似,都是用于字符串的輸出。命令格式:
echo string
您可以使用echo實(shí)現(xiàn)更復(fù)雜的輸出格式控制。
echo "It is a test"
這里的雙引號(hào)完全可以省略,以下命令與上面示例效果一致:
echo It is a test
echo "\"It is a test\""
結(jié)果將是:
"It is a test"
同樣,雙引號(hào)也可以省略
read 命令從標(biāo)準(zhǔn)輸入中讀取一行,并把輸入行的每個(gè)字段的值指定給 shell 變量
#!/bin/sh read name echo "$name It is a test"
以上代碼保存為 test.sh,name 接收標(biāo)準(zhǔn)輸入的變量,結(jié)果將是:
[root@www ~]# sh test.sh OK #標(biāo)準(zhǔn)輸入 OK It is a test #輸出
echo -e "OK! \n" # -e 開啟轉(zhuǎn)義 echo "It is a test"
輸出結(jié)果:
OK! It is a test
#!/bin/sh echo -e "OK! \c" # -e 開啟轉(zhuǎn)義 \c 不換行 echo "It is a test"
輸出結(jié)果:
OK! It is a test
echo "It is a test" > myfile
echo '$name\"'
輸出結(jié)果:
$name\"
echo `date`
注意: 這里使用的是反引號(hào) `, 而不是單引號(hào) '。
結(jié)果將顯示當(dāng)前日期
Thu Jul 24 10:08:46 CST 2018