Here is my script:
#/bin/bash
list="a b c"
for i in $list; do
echo $i
done
This works:
➜ ~ ./lol.sh
a
b
c
This doesn't:
➜ ~ . ./lol.sh
a b c
Why split does not work with dot command and how can I fix it?
Lists should never be represented as strings. Use array syntax.
list=( a b c )
for i in "${list[@]}"; do
echo "$i"
done
There are several reasons this is preferable.
setopt sh_word_split
, or using the parameter expansions ${=list}
or ${(ps: :)list}
hello[world]
, this will behave in an unexpected manner if your current directory contains files named hellow
, helloo
, or otherwise matching the glob).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With