Re: .env file handling

From: Colin Booth <>
Date: Tue, 24 Oct 2017 05:47:55 +0000

On Tue, Oct 24, 2017 at 09:25:37AM +0800, Casper Ti. Vector wrote:
> > #!/bin/sh
> > exec env -i $(cat /path/to/xyz.env) /path/to/xyz
> And of course you should be careful with the contents in `xyz.env'.
You can do the same with
while read A ; do
  export "$A"
done < "$1"
exec prog...

If you don't want to use cat.

I tried rewriting the whole thing in execline and while I'm pretty sure
it's doable it's not easy. The problem is that scope in execline doesn't
extend past the execution context of a given program, so while the
following program looks like it should work, it doesn't:

redirfd -r 0 $1
withstdinas -n VAR

since prog will get run with the environmental variable VAR set to the
entire contents of your file.

Changing it to the following to unwrap VAR ends up with you setting the
variables correctly each loop, but not getting everything pulled into
the environment at once due to the aforementioned scoping:

redirfd -r 0 $1
foreground {
  forstdin VAR
  importas -usd= VAR VAR
  export ${VAR}
  s6-echo ""

You can test that it's setting the variables and then losing them by
change `s6-echo ""' to `env'. And setting prog... to
`foreground { s6-echo "" } env'

Hope is however not all lost. You can do it with execline and s6 as long
as you have a tmpfs laying around somewhere:

foreground { mkdir -p /run/envdir }
redirfd -r 0 $1
foreground {
  forstdin -Cd"\n" VAR
  importas -u VAR VAR
  multidefine -d= ${VAR} { K V }
  redirfd -w 1 /run/envdir/${k}
  s6-echo ${V}
s6-envdir /run/envdir

That does create a directory somewhere, but it parses a multi-line K=V
style file into something that s6-envdir can handle.

Oh, and it should go without saying, but all these script snippets
assume that you're calling them as `script /path/to/envfile'


Colin Booth
Received on Tue Oct 24 2017 - 05:47:55 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC