2016-11-22 38 views
3

我搜索了很多東西,並且從來沒有得到任何與$ {parameter @ operator}一起工作的東西。我找到的所有鏈接都是相同的文檔。所以我認爲用實際例子給出一個適當的答案會對其理解非常有幫助。

文檔說:

$ {參數@操作}

擴張或者是參數還是取決於 運營商的價值有關參數本身的信息,的 價值的轉變。每個操作員是一個單一的字母:

Q
擴張是一個字符串,它是參數的在 格式,可以被重新用作輸入引述的值。

它還說,關於報價:

3.1.2報價
引用用來去除某些字符或單詞的特殊含義的外殼。

所以,我的理由是,這(特殊字符$)的輸出:

a="To be reused as an input string, \$0 needs to be quoted" 
echo ${[email protected]} 

應該是這樣的(在「」中,「\」被刪除,所以要使用需要再次引用作爲輸入):

To be reused as an input string, \$0 needs to be quoted 

,但我得到:

bash: ${[email protected]}: bad substitution 

我試過不同的組合:

${[email protected]}, "${[email protected]}", a='To be reused as an input string, $0 needs to be quoted' 

無濟於事。

實際上,我嘗試使用的任何運算符總是會產生錯誤的替換錯誤。這些似乎是bash非常模糊的特徵。我已經從這篇文章中省略了大約半小時的嘗試,每一個嘗試都比以前更扭曲!

回答

1

對於那些到達這裏尋找不同擴展操作符信息的人,下面是可用擴展字符及其效果的快速列表。

${[email protected]}返回Q uoted串與任何特殊字符(如\ N,\噸等)逃出。

實例:

$ foo="one\ntwo\n\tlast" 
$ echo "$foo" 
one\ntwo\n\tlast 

$ echo ${[email protected]} 
'one\ntwo\n\tlast' 

${[email protected]}返回與所有的轉義字符Ë xpanded一個字符串(例如\ N - >換行)。

例子:

$ foo="one\ntwo\n\tlast" 
$ echo "${foo}" 
one\ntwo\n\tlast 

$ echo "${[email protected]}" 
one 
two 
     last 

${[email protected]}返回一個字符串,顯示變量將是什麼樣子,如果它被用作P rompt變量(即PS1,PS2,PS3)

例如:

$ bar='host: \h' 
$ echo ${[email protected]} 
host: myhost1 

(還有更多的轉義序列th at可以應用於提示變量。參見bash documentation。)

${[email protected]}返回可用於 SSIGN可變與其現有名稱,值,並且如果任何declare選項的字符串。

實施例:

$ foo="test1" 
$ echo ${[email protected]} 
foo='test1' 

$ declare -i foo=10 
$ echo "${[email protected]}" 
declare -i foo='10' 

${[email protected]}返回字符串列出變量的悼念。

實施例:

$ declare -ir foo=10 
$ echo ${[email protected]} 
ir 

(可用DECLARE選項有:-r只讀,-i整數,-a陣列,-f功能,-x導出。)