0

There is a rise of .env files (nodejs community & even Python one).

They look like:

var1=val1
var2="val2 referencing ${var1}"

While there is special handling for export prefix I wonder if it is possible to process .env file by Bash so it exports all definitions without file containing export keyword...

I can imaging rudimentary form (without proper handling of edge cases):

while read line; do
  case "$line" in
    "export "*=*) eval "$line";;
    *=*) eval "export $line";;
  esac
done <.env
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • You could just source the `.env` file right? `. test.env` – 0stone0 Jun 01 '21 at 12:01
  • @0stone0 I could use `source`. The purpose is to *export values*. So local redefinitions are OK but they need to be exported too. – gavenkoa Jun 01 '21 at 12:02
  • 1
    If I'm not wrong `set -a` will auto export variable. So if you `set -a` before `source` the file, they should be exported. Trying to find an explanation – 0stone0 Jun 01 '21 at 12:04
  • 1
    [This](https://unix.stackexchange.com/questions/79064/how-to-export-variables-from-a-file/79077) and/or [this](https://unix.stackexchange.com/questions/428175/how-to-export-all-bash-functions-in-a-file-in-one-line) unix stackexchange questions show how to export using source, or 'all' the defined variables. – 0stone0 Jun 01 '21 at 12:05
  • 1
    @0stone0 Cool! I didn't know about `set -o allexport`. – gavenkoa Jun 01 '21 at 12:08
  • 1
    Glad you've learned something new today ;) Sound like this already has been answered on SO, therefore I didn't post this as an answer. Still looking for a dupe. – 0stone0 Jun 01 '21 at 12:09

0 Answers0