
이전에 이 질문에 대한 답변이 있었다면 죄송합니다. 검색을 시도했지만 해당 답변을 찾을 수 없습니다.
$a="hello"
$b="world"
$helloworldtest="worked"
echo "${a}${b}test"
- 인쇄 Hello{World}test
echo "${a}$btest"
- 인쇄 Hello
echo "$a$btest"
- 인쇄Hello
답변1
bash 명령을 사용하여 declare
동적 변수 이름을 만듭니다 .
$ a=hello
$ b=world
$ declare "${a}${b}test=worked"
$ printf ">>%s\n" "$a" "$b" "$helloworldtest"
>>hello
>>world
>>worked
네가 정말로 원할 때사용동적 변수 이름의 경우 다음을 사용해야 합니다.
- 간접변수
varname=${a}${b}test echo "${!varname}" # => worked
- 또는 nameref (bash 버전 4.3+)
declare -n ref=${a}${b}test echo "$ref" # => worked
그러나 연관 배열은 사용하기가 더 쉽습니다.
$ declare -A testvar
$ testvar[$a$b]="this works too"
$ echo "${testvar[helloworld]}"
this works too