Bash for loop with a list variable

Solved
martro1870 Posted messages 3 Status Membre -  
martro1870 Posted messages 3 Status Membre -
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 réponses

mamiemando Posted messages 33540 Registration date   Status Modérateur Last intervention   7 927
 
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