ドル記号について:ドル記号の中にドル記号がありますか?

ドル記号について:ドル記号の中にドル記号がありますか?

次のような結果があるとします。

echo $A
abc

echo $B
def

echo $abcdef
yesyes

AとBを使って「はい」をどうやって得るのですか?似たようなことを試しています。

${$A$B}        
$`echo "$A"$B`

しかし、それは失敗しました。どんな提案がありますか?

ベストアンサー1

Bashシェルを使用している場合は、中間変数を導入する限り、次のものを使用できます。間接的な:

$ echo $A
abc
$ echo $B
def
$ echo $abcdef
yesyes

それから

$ AB=$A$B
$ echo "${!AB}"
yesyes

変数間接参照は、Bashマニュアル()の**パラメータ拡張*セクションで説明されています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.

おすすめ記事