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 have an "init" file that reads

#!/bin/sh

export PATH=$PATH:/home/me/morph_numsys/software/bin

by which I'd just like to set the path to that folder for only this bash session. However, when I execute the script, sh init, it still doesn't recognize the path. However, if I just say export PATH=$PATH:/home/me/morph_numsys/software/bin, then it works fine. Any ideas how I can do this?

I'm on ubuntu and xterm.

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

When you say "for only this bash session", I assume you mean the bash session that is calling the script.

When you execute your script, it gets a new shell environment. Thus when you export variables, you are exporting it to the new shell environment and not its parent environment. As far as I know, there is no way to access the parent environment. However, since you are using bash, there may be a few solutions:

Use source

Rather than execute the script like this: /path/to/init.sh do source /path/to/init.sh

From the bash man page:

source filename [arguments]

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.

Make your script a shell function and put it in .bashrc

Another option is to make init a function and put it in .bashrc like this:

function init {
    export PATH=$PATH:/home/me/morph_numsys/software/bin
}

Then from the terminal you can just run init.

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.