LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   'export PATH' does not work (https://www.linuxquestions.org/questions/linux-newbie-8/export-path-does-not-work-283990/)

lemuel 01-30-2005 04:35 AM

'export PATH' does not work
 
hello everyone.

I made a simple test.sh script in my ubuntu box which contains 2 lines:

Code:

PATH=$PATH:/home
export PATH

however when I run the script, '/home' still cant be found in my path when i do an 'echo $PATH'.

but when I do an 'export PATH=$PATH:/home' on the command prompt, it works.

can anyone pls help me.

Dark_Helmet 01-30-2005 05:03 AM

When you run a script, the shell creates a new sub-shell. That's why it doesn't work. The PATH is updated in the sub-shell, but when the script finishes, that sub-shell and all of its settings (including environment variables) are lost. There is no way to "push" a sub-shell's settings into its parent.

You need to source the file instead of running it. Source'ing a file acts as if you input each line in the file directly at the command prompt. You can source a file in one of two ways:
Code:

source filename
or the short-hand:
Code:

. filename

lemuel 01-30-2005 09:51 AM

hey it worked thanks!

I've a few more questions though. i tried putting the same code in my .bash_profile then closed the terminal and opened another terminal. how come nothing happened. do i need to reset my pc for that?

sorry for this stupid question. i could have found out the answer by restarting the PC myself, but I cant and I dont have the liberty to shut it down.

slakmagik 01-30-2005 10:01 AM

It needs to be a login shell or in your .bashrc. No need to reboot - just logout/in or start a new term.

lemuel 01-30-2005 11:06 AM

hello

login shell in my .bashrc? what does that mean?

lemuel 01-30-2005 11:15 AM

hey hey I tried putting my code in .bashrc instead of in .bash_profile and it now works everytime I open up a new terminal.

does this mean that the "proper" way of setting up paths is to put them in .bashrc and not in .bash_profile?

another question. if putting stuff in .bash_profile doesn't do anything, what's the purpose of its existence?

btmiller 01-30-2005 11:24 AM

The .bash_profile script is invoked for login shells, and .bashrc for non-login shells (e.g. opening up a new xterm in X). Usually the way to do it is have one source the other so you have a consistent environment regardless of whether the shell is login or non-login.

lemuel 01-30-2005 11:39 AM

oooh ok. Thanks so much for the help you guys. I guess that's enough enlightenment for today.

I think what I love about linux is I learn so much every day. and the more I learn, the more I see how feeble WinXp is.


All times are GMT -5. The time now is 11:47 AM.