Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm trying to set environment variable through a shell script. Here is the script with various techniques:

echo "export SOMEPATH=/root/some" >> /root/.bash_profile
echo "export PATH=$PATH:$SOMEPATH/bin" >> /root/.bash_profile
source /root/.bash_profile

I run the script a.sh with sh a.sh

Then if I check the env variable it doesn't show up.

echo $SOMEPATH returns nothing. I expected to get SOMEPATH returned

share|improve this question

marked as duplicate by cuonglm, jasonwryan, Patrick, slm Jun 27 '14 at 12:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 22 down vote accepted

When you run a script it gets its own shell and its own environment, which disappears again as soon as the script is finished. To keep the environment variables around, source the script into your shell:

$ source a.sh

Then the definitions will be put into your current shell's environment and be inherited by any programs you launch from it.

share|improve this answer
thanks ! works just great! – hey Jun 27 '14 at 0:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.