Search code examples
bashsshquotesdouble-quotes

Escape single quotes ssh remote command


I read any solutions for escaping single quotes on a remote command over ssh. But any work fine.

I'm trying

ssh root@server "ps uax|grep bac | grep -v grep | awk '{ print $2 }' > /tmp/back.tmp"

AWK doesn't work:

ssh root@server "ps uax|grep bac | grep -v grep | awk \'{ print $2 }\' > /tmp/back.tmp"
....
awk: '{
awk: ^ caracter ''' inválido en la expresión

And I tried using single quotes on the command line, but they also didn't work.


Solution

  • In your first try you use double-quotes " so you need to escape the $ character:

    ssh root@server "ps uax|grep bac | grep -v grep | awk '{ print \$2 }' > /tmp/back.tmp"
                                                                   ▲
    

    Also, you can use:

     ps uax | grep 'ba[c]' | ...
    

    so then you don't need the grep -v grep step.