Search code examples
regexbashdockersedquoting

sed: unterminated `s' command - Can't figure out what needs to be escaped


I'm pretty new to using sed's regex so I have some environment variables that I need to be placed into a file if the appropriate 'placeholder' is found.

root@devproc-01:~# printenv
PROC_MODCONF=Include "conf.d/modconf.cfg.lua"
PROC_MODULES="lastlog"; "firewall"; "message_logging";

Here are my two defined environment variables, below are my sed commands, these are part of a Docker Compose entrypoint file.

sed -i s/'{$PROC_MODULES}'/$PROC_MODULES/g /etc/procdev/conf.d/modules.cfg.lua
sed -i s/'{$PROC_MODCONF}'/$PROC_MODCONF/g /etc/procdev/proc.cfg.lua

But when this script executes, I get the error seen in the title:

sed: -e expression #1, char 27: unterminated `s' command

I have double checked that my placeholder variables are indeed present and correct within those two files. This only happens with these two replacements, all of my other replacements work fine. :/ Does something need to be escaped?

Thanks!


As requested, a snippet of one of the config files so that you can see how the placeholder variables are displayed:

umask = "027";

Include "conf.d/modules.cfg.lua"

{$PROC_MODCONF}

Include "conf.d/c2s-s2s.cfg.lua"

Solution

  • Both the sed command and the env var contents need escaping.

    sed -i "s/{\\\$PROC_MODCONF}/$PROC_MODCONF/g" /etc/procdev/conf.d/modules.cfg.lua
    

    The sed command is in double quotes. That way bash evaluates the env vars in the double quoted string. So the second $PROC_MODCONF will be replaced with its value from the bash environment.

    We need to escape the first literal {$PROC_MODCONF} so that bash does not replace it with the value from the environment.

    Since the value of $PROC_MODCONF will be placed into the sed command verbatim, that also needs to be escaped.

    $ export PROC_MODCONF="Include\\ \"conf.d\/modconf.cfg.lua\""
    $ echo $PROC_MODCONF
    Include\ "conf.d\/modconf.cfg.lua"