Nested quotes bash [duplicate]

2019-01-15 18:58发布

问题:

This question already has an answer here:

  • Unix 'alias' fails with 'awk' command 3 answers

I want to nest multiple strings like this :

sudo ssh server "awk "/pattern/{print "hello"}1" file > file.tmp"

With 2 nested quotes I managed to make my command works :

awk "/pattern/{print \"hello\"}1" file > file.tmp

I cannot use single quote (') because there are variables in my command. Can someone help me ?

Thanks in advance.

回答1:

You can still place single quotes as long as the variables are intended to be initially expanded before the whole command string is executed on the shell on the remote server.

sudo ssh server "echo \"$SOMEVAR\"; awk '/pattern/{print \"hello\"}1' file > file.tmp"