When you run bash -c 'FOO=bar'
, a new bash
instance is spawned which executes the FOO=bar
command and then immediately terminates. The FOO
variable exists for a tiny fraction of a second and is then gone, like it never existed.
The example documentation you're referring to does something else, which is very similar to this:
bash -c 'export FOO=bar; run_some_entry_point'
The details are a little different; a script is executed using source
to define the variables and &&
is used to join the commands to avoid running the entry point if the source
command fails:
bash -c 'source file_containing_variable_exports && run_some_entry_point'
The most important point is that this is a single command executed by a single shell instance. The shell instance defines the variables which are marked for export, and then in that environment where those exported variables exist, it launches the container (or whatever) entry point.
This version of the above command will not work:
bash -c 'export FOO=bar'; run_some_entry_point
Here, the bash instance which defined the environment variable terminates, taking the variable away with it; the original shell runs the entry point, without that variable.
By the way, to run a command with some environment variables, you don't need to use a separate export
command. The shell has a syntax for that:
bash -c 'FOO=bar some_program arg ...'
Multiple variables can be given. These variables are not set into the shell's own environment; they are just added to the executed program.
FOO=BAR
to make it available as a shell variable in the "main" shell instance, orexport FOO=BAR
to make it available in that shell and in the environment of processes started from it. I'm not exactly at all sure why you'd run another shell here? – ilkkachu May 10 '22 at 20:14export SECRET_NAME=SECRET_VALUE
so that the container can rus the commandbash -c 'source /path/to/file' && npm start
and. Yet none of the secrets I set in this script are available in any shell I start on the container later on. – marsh May 10 '22 at 20:21bash -c 'source /path/to/file' && npm start'
the env vars set by the sourced script should be available tonpm
there. But there's no "global" environment where env vars would be set: they're only inherited to child processes, not to the parent, to to siblings. – ilkkachu May 10 '22 at 20:28npm start
usingprocess.env.VAR_NAME
, the variable isundefined
. I am very confused that this does not work given that I am doing the very same thing described in this example: https://www.vaultproject.io/docs/platform/k8s/injector/examples#environment-variable-example – marsh May 10 '22 at 20:50sh -c 'source somefile && run someprog'
, though. And yeah, that should still make the exported vars set in the file available to the program – ilkkachu May 11 '22 at 09:04