In bash
(works on v4
at least), the following commands allow to assign multiple variables from a string :
IFS=',' read a b <<< "s1,s2"
IFS=',' read a b < <(echo "s1,s2") # an equivalent
After one of these commands :
$ echo $a
s1
$ echo $b
s2
But provided commands are not POSIX-compliant; if run in sh
(dash
) :
sh: 1: Syntax error: redirection unexpected
What is a POSIX-compliant alternative to those commands? I tried :
IFS=',' echo "s1,s2" | read a b
The command succeeds (return code 0
), but echo $a
and echo $b
then prints nothing.