Is it possible to make the Bash "source" command keep environment variables passed in manually?

0

Issue

I have a file .env containing environment variables, e.g.:

TZ=UTC
NODE_ENV=development

Then, in my Bash scripts, I use:

set -o allexport; source .env; set +o allexport

This loads all the environment variables from .env.

However, if I manually pass environment variables to the script, they get overwritten by source. E.g. if I run NODE_ENV=production ./script.sh, I’d like it to keep NODE_ENV=production instead of using NODE_ENV=development from .env. How could I do this?

I’m using Ubuntu 20

Solution

I don’t think it is. But you can modify your .env file as follows to prevent it from overwriting NODE_ENV.

TZ=UTC
NODE_ENV=${NODE_ENV:-development}

Answered By – oguz ismail

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More