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?