0

I am trying to set environment variables from Bash as follow bash -c 'export FOO=BAR' but when I try to read the previously set variable using echo $FOO or bash -c 'echo $FOO' I can see that the variable is not initialized and the command returns an empty line.

How would I be able to set an environment variable from a command line using Bash to make it accessible from the same shell that started the program (i.e. bash -c 'export FOO=BAR') in a first place ?

thanasisp
  • 8,122
marsh
  • 1
  • 1
  • 1
    You'd run just FOO=BAR to make it available as a shell variable in the "main" shell instance, or export 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:14
  • I am trying to inject HashiCorp Vault secrets into a Pod's container environnement variables. In order to do so, all the secrets are written in a file in the following format export SECRET_NAME=SECRET_VALUE so that the container can rus the command bash -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:21
  • 1
    if you run bash -c 'source /path/to/file' && npm start' the env vars set by the sourced script should be available to npm 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:28
  • Note sure if there's more to your specific case, but in general, this is basically just a duplicate of Is it possible to pass environment variables from child to parent in user space? – ilkkachu May 10 '22 at 20:31
  • I believe so to, yet when I try to read any of the previously set environment variables in the Nodejs application launched by npm start using process.env.VAR_NAME, the variable is undefined. 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:50
  • Your examples use kubernetes, while you use bash. Those are not at all 'the very same thing', no more than a bicycle is the same thing as a snowboard. – dave_thompson_085 May 11 '22 at 03:00
  • @dave_thompson_085, the config file there, in the linked article, does have that sh -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

1 Answers1

1

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.

Kaz
  • 8,273