shell中的 echo命令


Shell echo命令

echo是Shell的一個內部指令,用於在屏幕上打印出指定的字符串。命令格式:

#所見即所得
echo arg

#echo是bash解釋器的內建命令
[root@hass-11 ~]# type echo
echo is a shell builtin

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

顯示轉義的字符

echo "\"It is a test\""
"It is a test"

echo -e "${color}txt${res}"

echo -e "OK! \nIt is a test"

雙引號也可以省略。

顯示變量

name="OK"
echo "$name It is a test"
OK It is a test

#同樣雙引號也可以省略

如果變量與其它字符相連的話,需要使用大括號{ }指定變量的界限:

mouth=8
echo "${mouth}-1-2009"

結果將是:
8-1-2009

顯示換行

echo -e "OK! \nIt is a test"

輸出:
OK!
It is a test

顯示不換行

echo -n "OK! It is a test"
for ((i=0;i < 5;i++));do echo -n ".";sleep 1;done

#不換行輸出

輸出:
OK!It si a test

顯示結果重定向至文件

echo "It is a test" > myfile

原樣輸出字符串

若需要原樣輸出字符串(不進行轉義),請使用單引號。例如:

echo '$name\"'

顯示命令執行結果

#優先執行 `` 內的命令
echo `date`

結果將顯示當前日期

從上面可看出,雙引號可有可無,單引號主要用在原樣輸出中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM