0

I have two configuration files for environment configuration, with content like this:

# First comment
hello='john'

# Second comment
test=3

i.e. a combination of comments, name=value pairs, and white space

Currently I have a reload script which replaces and copies their values to .bash_profile:

cat env.file1 | sed -e 's/#.*$//' -e '/^$/d' -e 's/^/export\ /' >> .bash_profile
cat env.file2 | sed -e 's/#.*$//' -e '/^$/d' -e 's/^/export\ /' >> .bash_profile

I would instead prefer if .bash_profile can instead "source" these two files, somehow performing the transformation in the process. That way it only takes one command (source .bash_profile) instead of two (env-reload && .bash_profile). Also it means the bash_profile can remain unmolested.

How to do this?

rgareth
  • 3,377
  • 5
  • 23
  • 35

1 Answers1

2

This question was answered here: Set Environment variables from file

The second answer down has the method I would use.

export `sed -e '/^\#/d' -e '/^$/d' env.file1 | xargs`

That works fine for one file, but what about more than one, like in your question? May I submit this:

for ENV_FILE in env.*; do export `sed -e '/^\#/d' -e '/^$/d' "$ENV_FILE"; done

If you would like to only update those variables, without reloading bash altogether, I suggest making an alias for it. Then, just type the name of the alias and reload them any time you want.

Hope this helps.

Community
  • 1
  • 1
xizdaqrian
  • 716
  • 6
  • 10