I have a written a bash script which uses number of parameters.
I have assigned variables in the following format.
./myscript.sh x y ...
You can use
read combined with herestring and the quote reconstruction ability of
read x y z k <<<$(printf " %q" "$@")
$ cat example.bash #!/bin/bash read x y z k <<<$(printf " %q" "$@") echo "x=[$x]" echo "y=[$y]" echo "z=[$z]" echo "k=[$k]" $ ./example.bash a b "c d" x=[a] y=[b] z=[c d] k=
So, what's going on here? Let's work from the inside out.
printf " %q" "$@" quotes the arguments it's given in a way equivalent to the original command line arguments. Without this quoting reconstruction, command line arguments with spaces would be treated as separate arguments even if originally quoted. To see what I mean, try sorontar's answer, just
read x y z w <<<"$@": z is assigned "c" and k is assigned "d".
read receives the reconstituted command line, then assigns every non-escaped-space separated string into the given variables, left to right.
Back to our example:
a b "c d"
printf " %q" "$@"is
a b c\ d
read x y z k <<<"a b c\ d"is a hard-coded representation of what you want.
While this is compact and extensible, it's also tricky. If your script takes arguments representing options (script behavior changes based on presence of absence of said arguments) then I'd suggest using
getopts. If, however, your script takes many arguments representing values (like inputs into a matrix calculation) then reading into an array (
read -a) might be easier to understand.
You might also want to handle the case where no command line arguments are provided. That requires a slight elaboration:
read x y z k <<<$([ 0 -eq $# ] && echo '' || printf " %q" "$@")
In this variant, the number of arguments are checked and if there are some, then the printf requoting business is performed.