不能在shell脚本中给变量$i赋值


abc=( "one" "two" "three" )
for((i=0; i<${#abc[@]}; i++))
{

xyz$i="hello"  --- this is giving me error as no such file or directory
eval xyz$i="hello" --- this is working fine and solving my above error
eval xyz$i="hello ${abc[$i]}"  --- this line is again giving me error as no such file or directory
}

如何正确赋值

您可以使用bash的printf内置-v选项:

#!/bin/bash
abc=( "one" "two" "three" )
for ((i=0; i<${#abc[@]}; i++)); do
printf -v xyz$i 'hello %s' "${abc[i]}"
done
echo "$xyz0"
echo "$xyz1"
echo "$xyz2"

相关内容

  • 没有找到相关文章