2

Goal: Set Environment variable from Bash script file and discoverable via Node process.env

Following effort works from macOS console, as would be expected:

I have created the following typescript file process.env.ts that outputs to console environment variable TEST_ENV_VALUE:

declare let process: {
    env: {
        TEST_ENV_VALUE: string;
    };
};

console.log('process.env.TEST_ENV_VALUE', process.env.TEST_ENV_VALUE);

package.json script contains:

  "scripts": {
    "test:process": "ts-node test/process.env.ts"
  },

When I perform the following directly within macOS console, environment variable TEST_ENV_VALUE is discoverable by process.env.ts:

$ unset TEST_ENV_VALUE
$ export TEST_ENV_VALUE="FOO"
$ echo $TEST_ENV_VALUE
FOO
$ npm run test:process
> ts-node test/process.env.ts

process.env.TEST_ENV_VALUE FOO

Success shows that process.env.TEST_ENV_VALUE returns value of FOO.

Attempt creating Bash Script File with same console script

The following bash script file set_env_variable.sh is attempting to replicate what was able to be performed within macOS console:

#!/usr/bin/env bash

USAGE="\nUsage: $0\n
[-h|--help]\n
--value\n
"

usage() { echo -e ${USAGE} 1>&2; exit 1; }

# read the options
OPTS=`getopt -o h --long help,value: -n 'set_env_variable.sh' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; usage ; exit 1 ; fi
eval set -- "$OPTS"

HELP=false
TEST_ENV_VALUE="TEST"

while true; do
  case "$1" in
    -h | --help )       usage ;;
    --value )           TEST_ENV_VALUE="$2" ; shift; shift ;;
    -- )                shift; break ;;
    * )                 break ;;
  esac
done

echo -e '\n-------------------------\n'
echo TEST_ENV_VALUE="${TEST_ENV_VALUE}"
echo -e '\n-------------------------\n'
unset TEST_ENV_VALUE
export TEST_ENV_VALUE="${TEST_ENV_VALUE}"

npm run test:process

However, when set_env_variable.sh executed, environment variable TEST_ENV_VALUE is not discoverable by process.env.ts:

$ unset TEST_ENV_VALUE
$ ./set_env_variable.sh --value "BAR"
-------------------------
TEST_ENV_VALUE=BAR
-------------------------
> [email protected] test:process /Users/jeffrey.tanner/github/ACT/act-qa
> ts-node test/process.env.ts

process.env.TEST_ENV_VALUE

In other words, process.env.TEST_ENV_VALUE returns undefined.

Assistance appreciated in creating a bash script that will export an environment variable that is discoverable by node process.env. Thank you

2
  • What happens when you run the script as #!/bin/bash as opposed to /usr/bin/env? Commented Dec 14, 2020 at 21:15
  • @RamanSailopal Thank you for your reply. Good idea. Same results. process.env.TEST_ENV_VALUE returns undefined Commented Dec 15, 2020 at 13:32

2 Answers 2

1

Why are you unsetting the variable then exporting it again?

unset TEST_ENV_VALUE
export TEST_ENV_VALUE="${TEST_ENV_VALUE}"

That, by definition, is exporting an empty string. Remove those two lines and replace

TEST_ENV_VALUE="TEST"

with

export TEST_ENV_VALUE="TEST"
Sign up to request clarification or add additional context in comments.

Comments

0

@KurisRader

I reworked based on your suggestion:

HELP=false
VALUE=""

while true; do
  case "$1" in
    -h | --help )       usage ;;
    --value )           VALUE="$2" ; shift; shift ;;
    -- )                shift; break ;;
    * )                 break ;;
  esac
done

echo -e '\n-------------------------\n'
echo TEST_ENV_VALUE="${VALUE}"
echo -e '\n-------------------------\n'

unset TEST_ENV_VALUE
export TEST_ENV_VALUE="${VALUE}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.