以下是我的代码段
counters=1
details='AAA BBB CCC'
details_$counters=$details
echo $details_1
预期结果
AAA BBB CCC
答案 0 :(得分:1)
使用eval
进行变量名称插值。
x="1"
eval "y_$x='hello world'"
echo $y_1 # outputs "hello world"
答案 1 :(得分:1)
使用declare
;它比eval
更安全(但不是完全安全),因为它限制了你可以运行的任意代码的数量。
declare "details_$counters=$details"
或者,您可以简单地使用数组:
all_details[$counters]=$details