[英]Not able to assign value to variable$i in shell script
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
}
我可以分配值的正确方法是什么
您可以使用带有-v
选项的 bash 内置的printf
:
#!/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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.