Bash String concatenation and get content

后端 未结 2 778
[愿得一人]
[愿得一人] 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
    
    0 讨论(0)
  • 2021-01-29 00:22
    var1="hello1"
    var2="hello2"
    var3="hello3"
    
    for i in `seq 1 3`;
    do
        ab=var$i
        echo ${!ab}
    done
    

    I'm not sure it is the best solution to your larger problem, but it is the direct solution to your immediate request.

    0 讨论(0)
提交回复
热议问题