I am trying to run a program like this:
$CMD $ARGS
where $ARGS is a set of arguments with spaces. However, zsh appears to be handing off the contents of $ARGS as a single argument to the executable. Here is a specific example:
$export ARGS="localhost -p22"
$ssh $ARGS
ssh: Could not resolve hostname localhost -p22: Name or service not known
开发者_如何学JAVAIs there a bash or zsh flag that controls this behavior?
Note that when I put this type of command in a $!/bin/sh script, it performs as expected.
Thanks,
SetJmp
In zsh it's easy:
Use cmd ${=ARGS}
to split $ARGS
into separate arguments by word (split on whitespace).
Use cmd ${(f)ARGS}
to split $ARGS
into separate arguments by line (split on newlines but not spaces/tabs).
As others have mentioned, setting SH_WORD_SPLIT makes word splitting happen by default, but before you do that in zsh, cf. http://zsh.sourceforge.net/FAQ/zshfaq03.html for an explanation as to why whitespace splitting is not enabled by default.
If you want to have a string variable (there are also arrays) be split into words before passing to a command, use $=VAR
. There is also an option (shwordsplit
if I am not mistaking) that will make any command $VAR
act like command $=VAR
, but I suggest not to set it: I find it very inconvenient to type things like command "$VAR"
(zsh: command $VAR
) and command "${ARRAY[@]}"
(zsh: command $ARRAY
).
It will work if you use eval $CMD $ARGS
.
精彩评论