달러 기호 정보: 달러 기호 안에 달러 기호가 있나요?

달러 기호 정보: 달러 기호 안에 달러 기호가 있나요?

다음과 같은 결과가 있다고 가정합니다.

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.

답변2

다음을 수행할 수 있습니다.

$ eval "echo \$$(echo ${A}${B})"
yesyes

위의 일반적인 형태는 eval "echo \$$(echo ...). 위의 코드는 변수를 ${A}${B}문자열로 변환 abcdef한 다음 이를 문자열로 평가합니다 echo \$abcdef.

떼어내면 eval중간 형태를 볼 수 있습니다.

$ echo \$$(echo ${A}${B})
$abcdef

그런 다음 eval변수를 확장합니다 $abcdef.

인용하다

관련 정보