This is my code to loop over colon separated values and do something with each value.
f()
{
IFS=:
for arg in $1
do
echo arg: $arg
done
}
f foo:bar:baz
This works fine in most POSIX compliant shells.
$ dash foo.sh
arg: foo
arg: bar
arg: baz
$ bash foo.sh
arg: foo
arg: bar
arg: baz
$ ksh foo.sh
arg: foo
arg: bar
arg: baz
$ posh foo.sh
arg: foo
arg: bar
arg: baz
$ yash foo.sh
arg: foo
arg: bar
arg: baz
But it does not work as expected in zsh.
$ zsh foo.sh
arg: foo:bar:baz
Is zsh in violation of POSIX here?
read -r -a args <<<"$1"
or such to read into an array, thenfor arg in "${args[@]}"; do ...
– Loathingzsh
does what POSIX should do, it POSIX hadn't been burdened with maintaining existing behavior as much as possible. As the worst possible solution, you could usesetopt SH_WORD_SPLIT
to restore the POSIX behavior. – Florey