Bash String concatenation and get content

后端 未结 2 782
[愿得一人]
[愿得一人] 2021-01-28 23:28

I have a bash loop, I\'m trying to read the all variables:

var1=\"hello1\"

var2=\"hello2\"

var3=\"hello3\"

for i in `seq 1 3`;
do
 ab=var$i
 # Now ab == var1,         


        
2条回答
  •  情话喂你
    2021-01-29 00:21

    Simpler approach:

    var1="hello1"
    var2="hello2"
    var3="hello3"
    
    eval echo\ $var{1..3}\;
    

    Is expanded to:

    echo $var1
    echo $var2
    echo $var3
    

    Ouput:

    hello1 
    hello2 
    hello3
    

提交回复
热议问题