1

In a shell script, what is the difference between the following?

export $(cat .env)

and

source .env

where .env is the name of a file with a key=value specified on each line. I'm not insterested in performance/speed benchmarks, only actual functionality/features. For context, I'm trying to make use of a docker-compose env file in supporting scripts.

Brian
  • 11
  • 1
  • The variable is only exported in the first case. This means that only in this case the variable with its value is available to the environment of a called program and the program can access its value. – Cyrus Feb 16 '18 at 04:39
  • It would be much safer to use `set -a` prior to `source` than relying on the unquoted command substitution to not perform any word-splitting or pathname expansion. – chepner Feb 16 '18 at 15:17

1 Answers1

-1

There's no difference from script perspective but the export option will avoid adding source .env to every script. So you could export the variables before running any script and also would help to have a single editing point.

LMC
  • 10,453
  • 2
  • 27
  • 52