1. 程式人生 > >Linux Shell中有三種引號的用法

Linux Shell中有三種引號的用法

containe ubunt ref highlight 處理 alt 返回 cin test

Linux Shell中有三種引號,分別為雙引號(" ")、單引號(‘ ‘)以及反引號(` `)。

其中雙引號對字符串中出現的$、‘‘、`和\進行替換;單引號不進行替換,將字符串中所有字符作為普通字符輸出,而反引號中字符串作為shell命令執行,並返回執行結果。具體含義如下:

雙引號(" "):在雙引號中,除了$, ‘‘, `和\以外所有的字符都解釋成字符本身。

單引號(‘ ‘):在單引號中所有的字符包括特殊字符($,‘‘,`和\)都將解釋成字符本身而成為普通字符。

反引號(` `):在反引號中的字符串將解釋成shell命令來執行。

舉例:

1 2 [email protected]:~
# echo "$PATH" /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

可以看到在雙引號中,$被作為特殊字符處理,PATH被解釋為變量。

[email protected]:~# echo ‘$PATH‘

$PATH

在單引號中,特殊字符也失去了特殊意義作為普通字符輸出。

[email protected]:~# echo ls

ls

ls是一個shell命令,直接echo ls shell會將ls作為普通字符輸出。如果我們加上反引號就不一樣了,

[email protected]

:~# echo `ls`

99.sh cloud_curr_design cloud_curr_design.tar.gz exefile for.sh gyb_virsh httpd-2.2.31 qemu_help readfile.sh switch.sh temp temp10.sh temp1.sh temp2.sh temp3.sh temp4.sh temp5.sh temp6.sh temp7.sh temp8.sh temp9.sh te.sh test9.sh ubuntu1204Server.img ubuntu1204Server.xml
ubuntuGuest.xml ubuntu-server.img win7.img

加上反引號之後,shell將ls作為命令執行,並將結果返回。

Linux Shell中有三種引號的用法