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?
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)./bin/sh? "(" unexpected is exactly what you'd get from dash, ash or similar POSIX shells from that code.Whilst I note the comment regarding lists by Charles Duffy, this was my solution/test.
#!/bin/zsh
function three()
{
first=$1
second=$2
third=$3
echo "1: $first 2: $second 3:$third"
}
setopt sh_word_split
set "1 A 2" "2 B 3" "3 C 4" "4 D 5"
for i;do
three $i;
done
This will output
1: 1 2: A 3:2
1: 2 2: B 3:3
1: 3 2: C 3:4
1: 4 2: D 3:5