How to write a bash script to set global environment variable?

Just run your shell script preceded by “.” (dot space).

This causes the script to run the instructions in the original shell. Thus the variables still exist after the script finish

Ex:

cat setmyvar.sh
export myvar=exists

. ./setmyvar.sh

echo $myvar
exists

Leave a Comment