4

I wrote a script to ssh to some nodes and run a sed command inside the node. The script looks like

NODES="compute-0-3"
for i in $NODES 
do
  echo $i
  ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'
done

However, the error is

unexpected EOF while looking for matching `''
syntax error: unexpected end of file

It seems that the character ' is not treated as the begining of a sed command.

mahmood
  • 23,197
  • 49
  • 147
  • 242

1 Answers1

10

I suggest to replace

ssh $i 'sed -i \'s/172.16.48.70/172.20.54.10/g\' /etc/hosts;'

by

ssh "$i" 'sed -i "s/172.16.48.70/172.20.54.10/g" /etc/hosts'

If you absolutely want to use single quotes:

ssh "$i" 'sed -i '"'"'s/172.16.48.70/172.20.54.10/g'"'"' /etc/hosts'
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • This might help: [How to escape single-quotes within single-quoted strings?](http://stackoverflow.com/q/1250079/3776858) – Cyrus Nov 19 '16 at 17:53