Shell腳本中的echo命令使用介紹
引言
Shell 的 echo 指令與 PHP 的 echo 指令類似,都是用于字符串的輸出。命令格式:
echo string
您可以使用echo實現更復雜的輸出格式控制。
1.顯示普通字符串:
echo "It is a test"
這里的雙引號完全可以省略,以下命令與上面實例效果一致:
echo It is a test
2.顯示轉義字符
echo ""It is a test""
結果將是:
"It is a test"
同樣,雙引號也可以省略
3.顯示變量
Read 命令從標準輸入中讀取一行,并把輸入行的每個字段的值指定給 shell 變量
#!/bin/sh read name echo "$name It is a test"
以上代碼保存為 test.sh,name 接收標準輸入的變量,結果將是:
[root@www ~]# sh test.sh
OK #標準輸入
OK It is a test #輸出
4.顯示換行
echo -e "OK! \n" # -e 開啟轉義 echo "It is a test"
輸出結果:
OK!
It is a test
5.顯示不換行
#!/bin/sh echo -e "OK! \c" # -e 開啟轉義 \c 不換行 echo "It is a test"
輸出結果:
OK! It is a test
6.顯示結果定向至文件
echo "It is a test" > myfile
7.原樣輸出字符串,不進行轉義或取變量(用單引號)
echo '$name"'
輸出結果:
$name"
8.顯示命令執(zhí)行結果
echo `date`
注意: 這里使用的是反引號 `, 而不是單引號 '。
結果將顯示當前日期
Thu Jul 24 10:08:46 CST 2014
Ead 命令一個一個詞組地接收輸入的參數,每個詞組需要使用空格進行分隔;如果輸入的詞組個數大于需要的參數個數,則多出的詞組將被作為整體為最后一個參數接收。
測試文件 test.sh 代碼如下:
read firstStr secondStr echo "第一個參數:$firstStr; 第二個參數:$secondStr"
執(zhí)行測試:
$ sh test.sh
一 二 三 四
第一個參數:一; 第二個參數:二 三 四
實例, 文件 test.sh:
read -p "請輸入一段文字:" -n 6 -t 5 -s password echo -e "\npassword is $password"
參數說明:
- -p 輸入提示文字
- -n 輸入字符長度限制(達到6位,自動結束)
- -t 輸入限時
- -s 隱藏輸入內容
$ sh test.sh 請輸入一段文字: password is asdfgh
以上就是Shell腳本中的echo命令的詳細內容,更多關于Shell腳本echo命令的資料請關注腳本之家其它相關文章!