Variable
關於美元符號:美元符號內的美元符號?
假設我們有:
echo $A abc echo $B def echo $abcdef yesyes
如何使用 A 和 B 獲得“yesyes”?我正在嘗試類似的東西:
${$A$B} $`echo "$A"$B`
但失敗了。有什麼建議嗎?
如果您使用的是 Bash shell,那麼只要您引入一個中間變數,您就可以使用間接:
$ echo $A abc $ echo $B def $ echo $abcdef yesyes
然後
$ AB=$A$B $ echo "${!AB}" yesyes
變數間接在 Bash 手冊 ( ) 的 *Parameter Expansion 部分中進行了描述
man bash
:If the first character of parameter is an exclamation point (!), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirec‐ tion.