Remote sed editing multiple hosts: How to quote

/ Published in: Bash
Save to your folder(s)

Here's a quick one-off trick for sed editing conf files on remote hosts. I will
post the core sed command first, showing the crazy quoting, then a for loop demonstrating how one might tackle multiple hosts. Took me a few tries to get the quoting so thought I'd save others the trouble.

In this example I had to change the homepage variable in a remote conf file.

* The URL needs single quotes in the conf file. '\'' does the trick.
* The End of Line metacharacter '$' needs to be escaped for bash. Like so, \$.
* Finally, note that the sed expression is wrapped in single quotes while the whole command is wrapped with double quotes. I tried other combos -- if you know why this is so, do tell.

Copy this code and paste it in your HTML
  1. ssh root@$host "sed -i 's#\(CONF_VAR=\).*\$#\1'\'''\''#' /etc/prog/prog-conf.conf"
  3. for host in 10.10.23.{11..33}
  4. do
  5. ssh root@$host "sed -i 's#\(CONF_VAR=\).*\$#\1'\'''\''#' /etc/prog/prog-conf.conf"
  6. done

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.