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.

I want to somehow enter a different bash shell with some altered environment variables.

For example, if I run script bfin.sh and it contains something like

export PATH=/home/me/bfin2012:$PATH

I want it to create a bash shell with this changed variable. How to do this?

share|improve this question
add comment

3 Answers

To load environment variables you've put into a file, you can use the source command. e.g.

See current path:

 > echo $PATH
 /bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin

File with custom environment settings..

 > cat exports
 export PATH="/home/me/bfin2012:$PATH"
 export ...

Load custom environment

 > source exports

Confirm changes.

 > env | grep '^PATH'
 PATH=/home/me/bin2012:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
share|improve this answer
add comment

You've already set the variable and exported the set variable. If you want to enter a new bash shell at this point with that variable present, you just run:

bash

Note that the new shell's startup procedure might end up overwriting your variable, though! This could happen in .bashrc, for example.

share|improve this answer
add comment

Either change your script to end with exec bash, or run

sh -c '. bfin.sh; exec bash'

If you want to change the environment of the current shell, run

. bfin.sh

The . (dot or period) builtin executes the command from the specified script inside the same shell environment, like a function.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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