In Bash, I can use the following code:
[ "$sshcmd" = "" ] && sshcmd="ssh -someopts myhost"
$sshcmd "echo hello world"
In ZSH, the same code does not work because it tries to find a "ssh -someopts myhost" executable. How can I do the same thing in ZSH?
Thanks, Albert
To split a string at whitespace (more generally, at $IFS
) like other shells: $=sshcmd
But you should instead make sshcmd
an array, so that your commands still works if one of the options contains whitespace:
sshcmd=(ssh -someopts myopts)
$sshcmd[@] "echo hello world"
This applies to bash and ksh too, by the way; but there you must also protect the array variable substitution against further splitting and filename expansion:
sshcmd=(ssh -someopts myopts)
"${sshcmd[@]}" "echo hello world"
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