Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active May 9, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/40d7bbab37348b00a64ae1489df8ac29 to your computer and use it in GitHub Desktop.
Save JoshCheek/40d7bbab37348b00a64ae1489df8ac29 to your computer and use it in GitHub Desktop.
Bash loops work differently with arrays vs multiline strings >.<
#!/usr/bin/env bash
-----() { echo "----- $1 -----"; }
----- 'ARRAY' -----
ary=()
ary+=(abc)
ary+=("d e")
for val in "${ary[@]}"; do
echo "val: $val"
done
# val: abc
# val: d e
----- 'Lines in `...`' -----
lines1=`ruby -e 'puts "ABC\nD E"'`
for val in "${lines1[@]}"; do
echo "val: $val"
done
# val: ABC
# D E
----- 'Lines in $(...)' -----
lines2=$(ruby -e 'puts "ABC\nD E"')
for val in "${lines2[@]}"; do
echo "val: $val"
done
# val: ABC
# D E
----- 'Lines in "$(...)"' -----
lines3="$(ruby -e 'puts "ABC\nD E"')"
for val in "${lines3[@]}"; do
echo "val: $val"
done
# val: ABC
# D E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment