I am trying to execute a single command over ssh
that contains `sub-code` or $(sub-code) (I use it all the time but I don't know the official name for that) to be executed first, but on the target server.
For the sake of argument, let's say this is the command I want to use. Ofcourse this can be done with hostname
, but this is just a simplified example that contains all the formatting wizardry I want to use.
echo `uname -a | awk '{print $2}'`
No problem. But how do you escape this properly to send it over ssh
in a single command? The following is wrong, because it lets the server reply your local hostname. The sub-code is executed locally:
ssh myServer echo `uname -a | awk '{print $2}'`
But any escape-ishness that comes to mind doesn't work:
$ ssh myServer echo \`uname -a | awk '{print $2}'\`
awk: cmd. line:1: {print $2}`
awk: cmd. line:1: ^ invalid char '`' in expression
$ ssh myServer echo \$(uname -a | awk '{print $2}')
bash: syntax error near unexpected token `('
$ ssh myServer echo \$\(uname -a | awk '{print $2}')
bash: syntax error near unexpected token `)'
$ ssh myServer echo \$\(uname -a | awk '{print $2}'\)
awk: cmd. line:1: {print $2})
awk: cmd. line:1: ^ syntax error
bash: -c: line 0: unexpected EOF while looking for matching `)'
bash: -c: line 1: syntax error: unexpected end of file
I would like an answer that includes using properly escaped ` or $() because I'd like to know if it's possible.
echo` could be something more complicated.