Source From Here
Question
If I defined environment variable(s) in a file, how do I export these variables all at once?
How-To
Assume we have a configuration file .env:
Then we can test it by
test.sh:
The execution result:
Run the following command, before setting the variables:
From
man page:
To turn this option off, run set +a afterwards. Let's update test.sh:
This time, the execution will look like:
Question
If I defined environment variable(s) in a file, how do I export these variables all at once?
How-To
Assume we have a configuration file .env:
- MY_NAME='John'
- MY_AGE='39'
- #!/bin/sh
- echo -e "===== Setting from .env ====="
- cat .env
- echo -e "\n===== My setting ====="
- env | grep 'MY'
- source ./.env
- echo -e "\n===== After .env, My setting ====="
- env | grep 'MY'
Run the following command, before setting the variables:
- set -a
To turn this option off, run set +a afterwards. Let's update test.sh:
- #!/bin/sh
- echo -e "===== Setting from .env ====="
- cat .env
- echo -e "\n===== My setting ====="
- env | grep 'MY'
- set -a
- source ./.env
- set +a
- echo -e "\n===== After .env, My setting ====="
- env | grep 'MY'
沒有留言:
張貼留言