Shell echo命令介紹

Shell 的 echo 指令與 PHP 的 echo 指令相似,都是用於字符串的輸出。

Shell echo命令介紹Shell echo命令介紹

Shell echo命令格式:html

echo string

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

1.顯示普通字符串:shell

echo "It is a test"

這裏的雙引號徹底能夠省略,如下命令與上面實例效果一致:htm

echo It is a test

2.顯示轉義字符blog

echo "\"It is a test\""

結果將是:字符串

"It is a test"

一樣,雙引號也能夠省略get

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

#!/bin/sh
read name 
echo "$name It is a test"

以上代碼保存爲 test.sh,name 接收標準輸入的變量,結果將是:table

[root@www ~]# sh test.sh
OK                     #標準輸入
OK It is a test        #輸出

4.顯示換行test

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.顯示命令執行結果

echo `date`

注意: 這裏使用的是反引號 `, 而不是單引號 '。

結果將顯示當前日期

Thu Jul 24 10:08:46 CST 2014

本文原創地址:https://www.linuxprobe.com/shell-echo-command.html

相關文章
相關標籤/搜索