Is it possible to escape quotes within escape quotes within escape quotes within escape quotes?

Doing three quotes doesn't help, you just unquote, quote, and unquote again. You also don't need the subshell.

What you really want is:

su - someuser -c 'ssh someplace "if ! grep \"some thing\" /etc/somefile; then doSomething; fi"'

This kind of approach can become unwieldy quickly -- consider just putting a script on your server and executing that. That would keep you far away from nested quote hell.


You can just use a here document.

su -someuser -c '<&1 >&2 ssh you@machine sh' 1<<\SCRIPT
    if   ! grep 'some spaces or whatever' /etc/somefile
    then : do something
    fi
#END
SCRIPT

The solution requires a little fd juggling because you'll need to get past the initial password read from su, and su won't pass on any file-descriptors above 2. But once you've authenticated su, ssh won't screw with i/o and so you can do the reassignment immediately and let your invoked shell on the other end read stdin.

Tags:

Shell

Ssh

Quoting