天天看點

Shell 中的反引号(`),單引号('),雙引号(")

在寫shell的時候老是傻傻分不清楚,今天來理一理。

1.反引号位 (`) 位于鍵盤的Tab鍵的上方、1鍵的左方。注意與單引号(')位于Enter鍵的左方的差別。

在Linux中起着指令替換的作用。指令替換是指shell能夠将一個指令的标準輸出插在一個指令行中任何位置。

如下,shell會執行反引号中的date指令,把結果插入到echo指令顯示的内容中。

[root@localhost sh]# echo The date is `date`      

The date is 2011年 03月 14日 星期一 21:15:43 CST

  

2.單引号、雙引号用于使用者把帶有空格的字元串指派給變量事的分界符。

  [root@localhost sh]# str="Today is Monday"
  [root@localhost sh]# echo $str      

  Today is Monday

如果沒有單引号或雙引号,shell會把空格後的字元串解釋為指令。

  [root@localhost sh]# str=Today is Monday
  bash: is: command not found      
  [root@localhost sh]# testvalue=100
  [root@localhost sh]# echo 'The testvalue is $testvalue'
  The testvalue is $testvalue
  [root@localhost sh]# echo "The testvalue is $testvalue"
  The testvalue is 100      

繼續閱讀