How to separate string into shell arguments?
shell, zsh
Solution
Zsh has `(z)` modifier:
ARGS=( ${(z)test_str} )
. But this will produce `echo` and `"a \" b c"`, it won’t unquote string. To unquote you have to use `Q` modifier:
ARGS=( ${(Q)${(z)test_str}} )
: results in having `echo` and `a " b c` in `$ARGS` array. Neither would execute code in ``…`` or `$(…)`, but `(z)` will split `$(false true)` into one argument.
that is to say:
% testfoo=${(z):-'blah $(false true)'}; echo $testfoo[2]
$(false true)
Problem
I have this test variable in ZSH: ``` test_str='echo "a \" b c"' ``` I'd like to parse this into an array of two strings `("echo" "a \" b c")`. i.e. Read `test_str` as the shell itself would and give me back an array of arguments. Please note that I'm not looking to split on white space or anything like that. This is really about parsing arbitrarily complex strings into shell arguments.