Bash for loop with a list variable

Solved
martro1870 Posted messages 3 Status Member -  
martro1870 Posted messages 3 Status Member -
Hello,

I am trying to create a loop with a variable:

var="test 01" "test 02" "test 03"
for elem in $var
do
echo $elem
done

doesn't work but

for elem in "test 01" "test 02" "test 03"
echo $elem
done

works fine

the question is how to write a list-type variable?
thank you for your help

2 answers

  1. mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 940
     
    You can write in toto.sh :

    #!/bin/sh list="tata toto titi" for element in $list do echo "element =" $element done


    Then, give execution rights on toto.sh and run the program :

    chmod a+x toto.sh ./toto.sh


    ... which will give :

    element = tata element = toto element = titi


    Good luck
    5