1

I have a variable which contains some text:

var=bar

I need to inject it before the followed pattern: </VirtualHost> in /etc/hosts/conf.d/ssl.conf.

When the variable contains a simple word it works fine:

bash-4.2$ sudo sed "/^<\/VirtualHost>.*/i${var}" /etc/httpd/conf.d/ssl.conf
...
</VirtualHost> 
bar
</VirtualHost> 

However when I try to assign an Apache config file to $var I got this error message:

bash-4.2$ var=$(sudo cat /etc/httpd/conf.d/myconf.conf)
bash-4.2$ sudo sed "/^<\/VirtualHost>.*/i${var}" /etc/httpd/conf.d/ssl.conf
sed: -e expression #1, char 69: unknown command: `S'

Am I missing something? Thanks for answers.

Jens
  • 69,818
  • 15
  • 125
  • 179
Caner
  • 72
  • 1
  • 7
  • 1
    See [this answer](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29613573#29613573), especially the section on "Escaping a string literal for use as the *replacement string* in `sed`'s `s///` command". – Gordon Davisson Dec 23 '21 at 02:41
  • Do you really have two `` tags, or has one of them an extra slash? – Jens Dec 23 '21 at 09:06
  • Without access to the file you are reading in, we can only speculate; but a better solution altogether is probably to use the `r` command in `sed`. I'm guessing the file contains a newline and so the next line of the value gets interpreted as a separate `sed` command. – tripleee Dec 23 '21 at 09:08

0 Answers0