SH File not setting ENV variables (Mac OS X)

3.2k views Asked by At

I have a file set_env.sh, that I run in the terminal using:

sh set_env.sh

The code runs, but the variables are not set. I am checking with

echo $EMAIL_SERVER

set_env.sh

#!/bin/sh

echo email Address
read y

echo email password?
read x

export EMAIL_SERVER='smtp.gmail.com'
export EMAIL_PORT=587
export EMAIL_DOMAIN='domain.com'
export EMAIL_AUTHENTICATION='plain'
export EMAIL_ENABLE_STARTTLS_AUTO=true
export EMAIL_USERNAME=$y
export EMAIL_PASSWORD=$x
2

There are 2 answers

2
Ruslan Osmanov On BEST ANSWER

Use source command instead:

source set_env.sh

The command executes the script in the current shell context as opposed to invocation via separate shell process: sh set_env.sh (the environment variables are set within the new sh process which is isolated from the current process).


By the way, you can use dot (.) instead of source, if you prefer.

0
Ale Part On

You may modify your ~/.bash_profile file in your home dir with this

export EMAIL_SERVER='smtp.gmail.com'
export EMAIL_PORT=587
export EMAIL_DOMAIN='domain.com'
export EMAIL_AUTHENTICATION='plain'
export EMAIL_ENABLE_STARTTLS_AUTO=true
export EMAIL_USERNAME=$y
export EMAIL_PASSWORD=$x