我正在尝试创建一个基于 $i 的变量
i=1
line="one two three four five six"
while [[ $i -lt 3 ]]; do
set string$i=`echo $line | cut -d" " -f1-3`
echo $string$i
do_stuff_here
done
当我这样做时,我得到以下输出
1
预期输出是
one two three
事实上,当我 echo $String1...我得到了预期的输出...所以它存储正确。 我知道这是我调用 $string$1 的方式...但我尝试了各种引号/括号,但它不起作用。有人可以告诉我如何调用我的变量吗?
最佳答案
除非您在 do_stuff_there 中增加 i ,否则您的 while 循环永远不会停止,无论如何,这应该更接近您的期望:
i=1
line="one two three four five six"
while [[ $i -lt 3 ]]; do
eval string$i=\"`echo $line | cut -d" " -f1-3`\"
eval echo \$string$i
do_stuff_here
done
关于shell - KSH Shell 双变量名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12503406/