I wish to replace ${var} placeholders in a nginx.conf file with env vars loaded from .env file (docker). I choose the ${var} syntax myself in line with docker-compose.

  • content .env file:

Doing sed string replace on this line e.g. is giving me headaches.

  • Text input:
    server_name ${DOMAIN_NAME};
  • command:
    source .env
    export DOMAIN_NAME
    sed -i "s/\${DOMAIN_NAME}/$DOMAIN_NAME/g" "$docker_root"/nginx/nginx.conf
  • desired outcome:
    server_name sub.domain.net;
  • but what I get is:
    server_name sub.domain.net

It doesn't even matter what follows the ${var} replacement. If I add a space and a word behind the placeholder, the word also ends up on the second line.

I used envsubst first and saw the same behavior there first. Then I tried with sed wich is doing the same :)

I tried a variety of escape slashes, single quotes and double quotes with no result.

What's going on here?


The problem was Windows style CRLF (carriage return, line feed) line endings in the files as I was working on Windows.

In response to Steeldriver's comment pointing that out,
I converted the files with dos2unix from CRLF to LF (linux & mac).

apt install dos2unix
find . -type f -print0 | xargs -0 dos2unix

The starting point was replacing NGINX placeholders with docker .env file variables. The NGINX built-in mechanism using envsubst wasn't working for me. These guys ran in the same issue.

For normal use cases, you can follow the instructions here.

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.