Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

#!/bin/sh
echo "export SOMEPATH=/root/some" >> /root/.bash_profile
echo "export PATH=$PATH:$SOMEPATH/bin" >> /root/.bash_profile
SOMEPATH=/root/some
export SOMEPATH
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 Gnouc, jasonwryan, Patrick, slm Jun 27 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.

1 Answer 1

up vote 6 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 at 0:08

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