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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With