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 ?

7
  • 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 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 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 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 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: vaultproject.io/docs/platform/k8s/injector/…
    – marsh
    May 10 at 20:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.