Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash declaratively defining a list to loop on

Tags:

bash

loops

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 (before the loop to keep it clean) 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 element in the array (EDIT read answers below as you can append to arrays.. I did not know you could).

How do others declaratively define lists in bash with out using separate files?

Sorry I forgot to mention I want to define the list at the top of the file before the for loop logic

like image 714
Adam Gent Avatar asked Mar 28 '26 19:03

Adam Gent


2 Answers

You can use the "HERE Document" like this:

while read a ; do echo "Line: $a" ; done <<HERE
123 ab c
def aldkfgjlaskdjf lkajsdlfkjlasdjf
asl;kdfj ;laksjdf;lkj asd;lf sdpf -aa8
HERE
like image 140
choroba Avatar answered Mar 30 '26 09:03

choroba


Arrays aren't so hard to use:

readarray <<HERE
this is my first line
this is my second line
this is my third line
HERE

# Pre bash-4, you would need to build the array more explicity
# Just like readarray defaults to MAPFILE, so read defaults to REPLY
# Tip o' the hat to Dennis Williamson for pointing out that arrays
# are easily appended to.
# while read ; do
#    MAPFILE+=("$REPLY")
# done

for a in "${MAPFILE[@]}"; do
    echo "$a"
done

This has the added benefit of allowing each list item to contain spaces, should you have that need.

like image 42
chepner Avatar answered Mar 30 '26 07:03

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!