Linux命令總結--echo命令
阿新 • • 發佈:2020-11-04
echo命令
Shell中的 echo 指令與 PHP 的 echo 指令類似,都是用於字串的輸出。
命令格式:
echo [選項] 內容
echo可以用為普通輸出和重定向輸出
普通輸出
1.顯示普通字串:(引號完全可以忽略)
[root@movie ~]# echo '我是個快樂的Carrie.' 我是個快樂的Carrie. [root@movie ~]# echo 我是個快樂的Carrie. 我是個快樂的Carrie. [root@movie ~]# echo "我是個快樂的Carrie." 我是個快樂的Carrie. [root@movie ~]#
2.如果不想忽略,且想顯示,需要轉義字元
[root@movie ~]# echo "\"我是個快樂的Carrie."\" "我是個快樂的Carrie."
3.顯示變數
read 命令從標準輸入中讀取一行,並把輸入行的每個欄位的值指定給 shell 變數
[root@movie ~]# source test.sh It is a test [root@movie ~]# cat test.sh #! /bin/bash i=name echo "$name It is a test"
4.echo -e(-e 輸出轉義字元)
不加-e選項的結果 [root@movie ~]# sh test.sh It is a test \n 這是一個測試 [root@movie ~]# cat test.sh #! /bin/bash i=name echo "$name It is a test \n 這是一個測試" 加-e的結果[root@movie ~]# sh test.sh It is a test 這是一個測試 [root@movie ~]# cat test.sh #! /bin/bash i=name echo -e "$name It is a test \n 這是一個測試"
常用的轉義字元如下:
\b 轉義後相當於按退格鍵(backspace) ,但前提是"\b"後面存在字元
\c 不換行輸出,在"\c"後面不存在字元的情況下,作用相當於echo -n
\n 換行
\f 換行
\v 與\f相同;
\t 轉以後表示插入tab,即製表符
\r 游標移至行首,但不換行,相當於使用"\r"以後的字元覆蓋"\r"之前同等長度的字元,只看這段文字描述的話可能不容易理解
\\ 表示插入"\"本身;
6.引號不同結果會不相同
[root@movie ~]# echo 'date' date [root@movie ~]# echo `date` 2020年 11月 04日 星期三 18:38:24 CST
注意:第一個是單引號,第二個是反引號
重定向輸出
重定向輸出顧名思義就是改變其輸出的位置
1.將內容重定向到檔案中
需要注意的是重定向後,檔案原來的內容就會被覆蓋
[root@movie ~]# cat test.sh #! /bin/bash i=name echo -e "$name It is a test \n 這是一個測試" [root@movie ~]# echo 'Carrie' > test.sh [root@movie ~]# cat test.sh Carrie