Last active
May 9, 2017 19:34
-
-
Save JoshCheek/40d7bbab37348b00a64ae1489df8ac29 to your computer and use it in GitHub Desktop.
Bash loops work differently with arrays vs multiline strings >.<
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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