2

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.

8

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.

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.