Reputation: 1889
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
Upvotes: 2
Views: 3761
Reputation: 1889
@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}"
Upvotes: 0
Reputation: 7469
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"
Upvotes: 1