Unzip a string in a variable

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 the executable file "ssh -someopts myhost". How can I do the same in ZSH?

Thanks Albert

+5
source share
1 answer

To split a line into a space (in general, into $IFS), like into other shells:$=sshcmd

But instead, you should make an sshcmdarray so that your commands still work if one of the options contains spaces:

sshcmd=(ssh -someopts myopts)
$sshcmd[@] "echo hello world"

This applies to bash and ksh, by the way, by the way; but there you should also protect the replacement of the array variable from further splitting and expanding the file name:

sshcmd=(ssh -someopts myopts)
"${sshcmd[@]}" "echo hello world"
+2
source

All Articles