使用其他变量的聚合值获取变量的值

Get value of variable with the aggregated values of other variables

鉴于:

export A='TEST_'
export B='VAR'

在这种情况下,我如何获得 $TEST_VAR 的值?

更多条件:

  • should work both on sh and bash of the latest versions.
  • should not use any not pre-installed ubuntu dependencies.
  • should be the simplest one liner solution

可以使用间接变量引用:

test_var='foo bar baz'
a='test_'
b='var'
c="${a}${b}"

echo "${c}"
test_var

echo "${!c}"
foo bar baz

PS: 你应该在 Unix 中避免所有大写变量以避免与 shell 内部变量冲突。