5

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?

5

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
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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