1

How do I loop over a constant list in bash, where the list is separated by new lines? Something like:

for name in (
    aaaaaaaa
    bbbbbbbb
    cccccccc
); do
    echo $name
done

Expect this to output:

aaaaaaaa
bbbbbbbb
cccccccc
2
  • Does "constant" mean you can hardcode it in the script? Sep 5, 2019 at 10:17
  • @KamilMaciorowski Yup
    – simonzack
    Sep 5, 2019 at 10:19

1 Answer 1

3

Define an array:

values=(
    aaaaaaaa
    bbbbbbbb
    cccccccc
)

for name in "${values[@]}"; do
    echo $name
done

Or use \ line joiner:

for name in \
    aaaaaaaa \
    bbbbbbbb \
    cccccccc \
; do
    echo $name
done
1
  • Thanks very much! Look very familiar, probably used this once upon a time but forgot about them long ago.
    – simonzack
    Sep 5, 2019 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .