i cant get this to work in bash/rhel 7:

ssh host "sed -i -e \"s/dnl MASQUERADE_AS.*\$/MASQUERADE_AS(\`domain.com\')dnl/\" /etc/mail/sendmail.mc"

bash: -c: line 0: unexpected EOF while looking for matching ``'
bash: -c: line 1: syntax error: unexpected end of file

i essentially want this sed which works on a regular command line but breaks within ssh:

sed -i -e "s/dnl MASQUERADE_AS.*$/MASQUERADE_AS(\`domain.com\')dnl/" /etc/mail/sendmail.mc

anyone have any ideas? much appreciated.

  • Is that supposed to be a backtick? That's the start of the shell command substitution syntax. Quoting hell is no fun: try this ssh host 'sed -i -e "s/dnl MASQUERADE_AS.*\$/MASQUERADE_AS(\`domain.com'\'')dnl/" /etc/mail/sendmail.mc' Nov 27 '19 at 17:55
  • 1
    @glennjackman sendmail.mc is under m4 macro assembler, it might be using a peculiar syntax.
    – Archemar
    Nov 27 '19 at 18:14
  • 2
    Consider using a quoted here-string as input to ssh host -- see unix.stackexchange.com/a/453050/117549 as one example
    – Jeff Schaller
    Nov 27 '19 at 18:26

Using Jeff's idea of using a here-document for the sed script itself (well, he mentions a "here-string", but you'd have issues with quoting there too I think):

ssh host 'sed -i -f - /etc/mail/sendmail.mc' <<'SED_END'
s/dnl MASQUERADE_AS.*/MASQUERADE_AS(`domain.com')dnl/

If the sed on the remote host can read a sed script from standard input using -f -, then this would probably work (otherwise, for example on some BSD systems, try -f /dev/stdin).

The command calls sed on the remote host and passes the sed editing script on standard input. The sed script is single quoted due to the quotes in 'SED_END', so the shell won't be trying to expand things in its text.

I've also removed the trailing $ in the regular expression as it's not needed (.* would match to the end of the line anyway).

A ever so slightly shorter sed script (just avoiding mentioning MASQUERADE_AS twice):

s/dnl \(MASQUERADE_AS\).*/\1(`domain.com')dnl/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.