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"