I have an nginx conf file that due to using environment variables, I have a shell script echo, this file I refer to as nginx.conf.template, then I place the echoed result into the actual nginx.conf, see shell script bellow:

function subst() { eval echo -E "$2"; }
mapfile -c 1 -C subst < nginx.conf.template > nginx.conf

The only issue I have with this is that it removes all whitespace for indentation, I imagine this would cause an issue, but the docker container that I am running this nginx server in seems fine. Does it matter that I have no indentations? If it does matter, can echo preserve said whitespace?

  • I ended up just using confd, much better than this hack, but chicks did answer the question at hand I guess – Christian Grabowski Aug 25 '15 at 19:17

It isn't removing all of your whitespace. It is reducing each chunk of whitespace to a single space. For most of your nginx.conf this won't hurt anything. If you have quoted strings with multiple spaces those will get changed too by the command above. You can check for that easily by greping for quotes in the template.

Rather than tweaking echo to preserve whitespace it might be easier to build your template as a heredoc in the script.

Getting rid of the extra whitespace could be useful to reduce memory footprints in an embedded environment, but unless you are severely resource constrained maintaining the formatting from template to actual configuration will make debugging much more pleasant.

  • Sorry I should clarify, and will, I meant the whitespace in my indentation. – Christian Grabowski Jun 26 '15 at 18:18
  • the way your shell script is written it does not care whether it is whitespace at the beginning, middle, or end of the line. – chicks Jun 26 '15 at 18:22

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.