任何想法为什么会这样?为什么我必须手动显式重新分配变量但如果我在变量名称中有另一个变量则不能这样做?
脚本:
#!/bin/bash
a_1=1
a_2=1
for temp in 1 2
do
a_$temp="2"
echo $((a_$temp))
done
a_1=2
a_2=2
echo $a_1
echo $a_2
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
[dgupta@della4 Rates_Of_Quenching]$ ./test.sh
./test.sh: line 8: a_1=2: command not found
1
./test.sh: line 8: a_2=2: command not found
1
2
2
Run Code Online (Sandbox Code Playgroud)
代替:
a_$temp="2"
Run Code Online (Sandbox Code Playgroud)
使用:
declare a_$temp="2"
Run Code Online (Sandbox Code Playgroud)
用动态名称创建变量.