1. 程式人生 > >Shell程式設計四-Shell echo命令

Shell程式設計四-Shell echo命令

Shell echo命令

Shell 的 echo 指令與 PHP 的 echo 指令類似,都是用於字串的輸出。命令格式:
echo string

可以使用echo實現更復雜的輸出格式控制。

1.顯示普通字串:
echo "C++ is the best language!"

這裡的雙引號完全可以省略,以下命令與上面例項效果一致:
echo C++ is the best language!

2.顯示轉義字元:
echo \"C++ is the best language!\"
或者:
echo "\"C++ is the best language!\""

3.顯示變數
read 命令從標準輸入中讀取一行,並把輸入行的每個欄位的值指定給 shell 變數

4.顯示換行
echo -e "\"C++ is the best language!\" \n"

5.#顯示不換行
echo -e "\"C++ is the best language!\" \c"
echo "abc"

6.顯示結果定向至檔案
echo "C++ is the best language!" > file.txt

7.原樣輸出字串,不進行轉義或取變數(用單引號)
echo '$name\"'

8.顯示命令執行結果
echo `date`

例項:
#!/bin/bash
#--------------------------------------------
#name:practise20.sh
#author:wdh
#date:20181207
#--------------------------------------------

#顯示普通字串
echo "C++ is the best language!"

#省略雙引號
echo C++ is the best language!

#顯示轉義字元
echo \"C++ is the best language!\"

#顯示變數
echo "Please enter a line of characters:"
read name

#顯示換行
#-e 轉義開啟
echo -e "\"C++ is the best language!\" \n$name"

#顯示不換行
#\c 不換行
echo -e "\"C++ is the best language!\" \c"
echo "$name"

#顯示結果定向至檔案
echo "C++ is the best language!" > file.txt

#原樣輸出字串,不進行轉義或取變數(用單引號)
echo '#name\"'

#顯示命令執行結果
echo `date`
#!/bin/bash
#practise20.sh

#顯示普通字串
echo "C++ is the best language!"

#省略雙引號
echo C++ is the best language!

#顯示轉義字元
echo \"C++ is the best language!\"

#顯示變數
echo "Please enter a line of characters:"
read name

#顯示換行
#-e 轉義開啟
echo -e "\"C++ is the best language!\" \n$name"

#顯示不換行
#\c 不換行
echo -e "\"C++ is the best language!\" \c"
echo "$name"

#顯示結果定向至檔案
echo "C++ is the best language!" > file.txt

#原樣輸出字串,不進行轉義或取變數(用單引號)
echo '#name\"'

#顯示命令執行結果
echo `date`

執行 ./practise20.sh
執行指令碼,結果如下:
C++ is the best language!
C++ is the best language!
"C++ is the best language!"
Please enter a line of characters:
abcd
"C++ is the best language!" 
abcd
"C++ is the best language!" abcd
#name\"
2018年 12月 07日 星期五 21:04:42 CST