I'm trying to write a script that requires to export some env variables by reading the values and names from an .env
file.
Here's an example of the .env
file:
INSTANCE_PROFILE_ARN=arn:aws:iam::1234567890:instance-profile/development-west
SERVICE_NAME=reports
PIPELINE_CRON='0 0 0/6 * * ?'
And in the script.sh
file I'm exporting the variables using the following code:
if [ -f "./.env" ]; then
echo "** Using data from .env file **"
export $(cat .env | xargs)
fi;
But I'm getting this error:
➜ ./script.sh
./script.sh: line 5: export: `0': not a valid identifier
./script.sh: line 5: export: `0/6': not a valid identifier
./script.sh: line 5: export: `README.md': not a valid identifier
./script.sh: line 5: export: `demo.py': not a valid identifier
./script.sh: line 5: export: `script.sh': not a valid identifier
./script.sh: line 5: export: `?': not a valid identifier
PIPELINE_CRON=0
I've been testing some solutions I found in other post but is not working.
Using the -d '\n'
option will not work as I am working on macOS:
➜ xargs -d
xargs: illegal option -- d
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]
[-J replstr] [-L number] [-n number [-x]] [-P maxprocs]
[-s size] [utility [argument ...]]
If I'm understanding your requirement correctly, you may want to say:
if [[ -f ".env" ]]; then
echo "** Using data from .env file **"
set -a # set "allexport" option
source ".env" # the variables are exported
set +a # reset "allexport" option
fi
# export -p # uncomment to see exported variables