簡體   English   中英

無法在 shell 腳本中為變量 $i 賦值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM