如何使用计数器创建新变量?

时间:2017-02-03 09:25:04

标签: bash shell scripting

以下是我的代码段

counters=1
details='AAA BBB CCC'
details_$counters=$details
echo $details_1  

预期结果

AAA BBB CCC

2 个答案:

答案 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