0

Docker env files look similar to shell scripts defining variables.

Unfortunately in env files, values cannot be quoted and so simply sourcing them only works if there are no "special" characters in the values.

VAR1=This_works
VAR2=This will not work
VAR3=This won't either

Is there any way you can use these files in a shell script?

My current approach is this:

eval $( perl -ne '
  s/\x27/\x27\\\x27\x27/g;
  s/^(\w+)=(.+)$/export $1=\x27$2\x27/ and print
' "path/to/env_file" )

So I'm searching for any quote in each line of the env file and replace it by '\''.

Then I'm modifying each line which starts with an identifier (\w+) followed by a = and any text (.+). So the VAR3 will become: export VAR3='This won'\''t either.

The modified line is then printed.

Everything which was printed is eval-ed and so the variables will be available in my shell's environment.

Are there other proposals how to achieve this?

Skeeve
  • 7,188
  • 2
  • 16
  • 26
  • 1
    [This answer](https://stackoverflow.com/a/56229034/10008173) to [Set environment variables from file of key/value pairs](https://stackoverflow.com/questions/19331497/set-environment-variables-from-file-of-key-value-pairs) might be what you want. – David Maze Oct 29 '20 at 14:12
  • Here's a lot going on in that thread. Except for the `-a` to export all variables, it doesn't seem to contain anything new. Using sed and grep instead of perl doesn't seem to make a huge difference. Thanks anyways. – Skeeve Oct 29 '20 at 15:18

0 Answers0