In bash, I often create scripts where I iterate over the list of strings that I define.
eg.
for a in 1 2 3 4; do echo $a; done
However, I would like to define a list (before the loop saves it) so that it contains spaces without a separate file:
eg. (BUT THIS DOES NOT WORK)
read -r VAR <<HERE
list item 1
list item 2
list item 3
...
HERE
for a in $VAR; do echo $a; done
Expected result 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 need to index every element in the array (EDIT reads the answers below, as you can add to arrays .. I did not know what you could).
How do others declaratively define lists in bash without using separate files?
Sorry to forget to mention that I want to define a list at the top of the file before the loop loop logic
source
share