Skip to content

Linux Bash scripting: multi-line command and iterating over an array

It’s simple, really, but I rarely use bash so I’m always a little lost when I try to do something not absolutely trivial in it…
I’ll just put my script here and explain below:

images=("pic 1.jpg" \
"pic 2.jpg" \
"pic 3.jpg")

for i in "${images[@]}"
   echo $i
   node ../path/to/myscript.js --source "$i"

What it does is:
– define an array: myArray = ( elem1 elem2 )
– use multiple lines. To do so: when you split your command over multiple lines, end each non-end line with “\” to indicate continuation on the next line
– then iterate over it, echoing each element and running a command (with quotes because the elements of my array contain spaces)

This (very recent) source was helpful: Bash For Loop Array: Iterate Through Array Values

Posted in Linux.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA