'Create .env file with shell script - how to wrap value in commas?
I am currently using the following to make a .env
to help me with with my environment variables when using docker > supervisor > cron.
printenv > /var/www/laravel.env
This prints a list which is perfect (except issues with those with spaces in...)
I know that I can use sed
to seperate them at the =
sign and then wrap the second part in speech marks.
For example..
MY_VAR="MY VALUE WITH SPACES WORKS"
Solution 1:[1]
The output of printenv
can't be used to reliably reconstruct the values of all possible environment variables. Consider this output:
foo=bar
baz=3
Is that the result of export foo=bar baz=3
or export foo=$'bar\nbaz=3'
? There is no way to know.
In fact, I do not know of a portable way to reproduce any valid environment so that it can be reconstructed. The closest thing I can think of is in bash
, you can use declare -px
to produce output which can be used as the target of a source
command to reproduce values whose names are valid identifiers.
Solution 2:[2]
This will work, unless the values contain also =
printenv |awk -F"=" '{print $1"=""\""$2"\""}'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | chepner |
Solution 2 | George Vasiliou |