Operating System - HP-UX
1832647 Members
2839 Online
110043 Solutions
New Discussion

Re: set Path in script not working

 
SOLVED
Go to solution
Low Han Ming
Advisor

set Path in script not working

Hi,

I have a simple yet fustrating problem in executing a set PATH command in my script.

e.g.
PATH=/home:$PATH
export PATH
echo $PATH

the echo $PATH is for the purpose of debugging.
When I run the script, the echo statement reflect that /home is added to the PATH.
But once, the script is finished executing when I did an echo $PATH at the commandline, the /home isn't in the PATH.

Can anyone kindly help?

Thanks a million.


Han Ming
6 REPLIES 6
Denver Osborn
Honored Contributor

Re: set Path in script not working

When the script is finished the shell the script was run in is also finished, that's my understanding of why you can type echo $PATH and not see /home after the script is run. Have it open a shell at the end of your script to see what I mean.

Add /sbin/sh to end of script then type echo $PATH, type exit to close the shell and finish script.
James R. Ferguson
Acclaimed Contributor

Re: set Path in script not working

Hi:

Your script executes in its own environment. While this environment includes your shell's variables it is destroyed upon exit. The PATH variable is treated as a local variable (i.e. not global) when referenced by your script. Stated differently, your script gets a copy of the global variable. The behavior you describe is expected.

...JRF...
Low Han Ming_1
New Member

Re: set Path in script not working

Thanks.

In this case, how do I set the global variable.

Thanks again for the advice.


Han Ming
Tk Lau
New Member
Solution

Re: set Path in script not working

Hi Han Ming

If you want the variable setting be valid in you current shell, you need to put a dot before the script name . e.g.
#. script.sh

the dot tells the shell to source the environment variable setting inside the script to be the global variable of the current shell.
Low Han Ming
Advisor

Re: set Path in script not working

Hi,

Thanks for all the help.

I guess it has solved my problem totally.

Thanks very much once again.


Han Ming
Tim Nelson
Honored Contributor

Re: set Path in script not working

I believe the export of the PATH variable is only for subsequent shell environments. If you exit the script/shell program you are reverting back to the previous environment where /home was not part of the the PATH variable. So by setting the PATH variable in the script it would be available to other scripts or programs called by the script but not available to the parent script.