Monday, May 21, 2012

Bash declaratively defining a list to loop on

In bash I frequently make scripts where I loop over a list of strings that I define.



e.g.



for a in 1 2 3 4; do echo $a; done


However I would like to define the list so that it contains spaces and with out a separate file:



e.g. (BUT THIS WILL NOT WORK)



read -r VAR <<HERE
list item 1
list item 2
list item 3
...
HERE

for a in $VAR; do echo $a; done


The expected output above (I would like):



list item 1
list item 2
list item 3
etc...


But you will get:



list
item
1


I could use arrays but I would have to index each array. How do others declaratively define lists in bash with out using separate files?





No comments:

Post a Comment