Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I export PATH=$PATH:/newdir using script

Status
Not open for further replies.

dans45

Technical User
Feb 13, 2003
43
0
0
US
Hello
I'm running as root on KNOPPIX cd. I want to run a shell script to append to the PATH. If I enter "export PATH=$PATH:/newdir" at the command line it works. If I try to execute in my script it fails. Is there a simple way to do this in my script?
If I append to /etc/profile I have to rerun the profile from my script. I don't want to do that.
Thanks,
Dan
 
Thanks Feherke for the reply however I don't know exactly how to change my PATH using the source command. Sorry, I'm a novice. For now I am appending my PATH=xxxx to /etc/profile and executing 'su root'in my script which makes the changed profile active. Can you explain a bit how the source command changes my PATH. Thanks
Dan
 
When you say "I try to execute in my script it fails", how exactly does it fail? Does it give you an error message, or does it simply seem to have no effect?

When you run a shell script on Unix it actually creates a new shell process to run that script. Any PATH changes you make will last for the lifetime of that process, and when it terminates (i.e. when your script completes), they are lost. The parent shell never knows that it changed.

What Feherke suggested was that you "source" your script instead of running it normally. That means that the commands are read in by your currently running shell and executed, and any changes to PATH or other environment variables are retained for the lifetime of your current shell... i.e. until you type exit.

Annihilannic.
 
It should be as easy as

#/usr/bin/ksh

# display PATh before change
echo $PATH
PATH=$PATH:/tmp
echo $PATH

remember the change will only last for the time the script runs.

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
To do it from command line

export PATH=$PATH:/tmp


This will last for the duration of your session.

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Thanks, I did not know that it would only last the duration of my script. I'm just learning.
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top