Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
因此,这类似于通过引用传递参数。我想访问一个变量(回声),它的名称是来自不同字符串的组合字符串。一个简单的例子如下:
A1=999 n="1" B="A$n"
我想要的是,当我这样做时echo $B,它会返回999。如果需要进一步解释,请告诉我。谢谢。
echo $B
999
您正在寻找间接
echo ${!B}
来自 bash 手册
${!prefix*} ${!prefix@} Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable.
你也可以这样做
eval echo "$"$B
但凯文的答案肯定更好。