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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing environment variables 2

Status
Not open for further replies.

630111

MIS
Oct 30, 2001
127
US
Running Solaris 8 here.

When I log in, my path is set to a list of directories. I need to write a script that will change the path and when it exits, retains the new path. Is this even possible? What would be a better approach?

Thanks!
630111
 
Simply source (the dot command) the script:
. /path/to/script.sh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You probably know this, but just making sure: the usual place to set your PATH is in a shell startup file such as [tt]~/.profile[/tt] or [tt]~/.cshrc[/tt].

It is impossible for a process to modify the environment of its parent process. That's why you have to "source" the script for the change to take place; a script that is sourced is executed directly by the calling shell, not by a subprocess.

Oh, and if the dot command shown above doesn't work, the [tt]source[/tt] command might. Depends on your shell family.
 
Ahhh, I didn't understand you both at first so I had to read it a few times.

I created a script called testpath.sh. This is it...
#!/bin/sh
PATH=/export/home/me
export PATH

So I log in and check my current path

bash-2.03$ echo $PATH
/usr/dt/bin:/usr/bin:/usr/ucb:/etc:

I run my script. Note the syntax. That is a dot, followed by a space, followed by the entire path to the script.

bash-2.03$ . /export/home/me/testpath.sh

I check the path again

bash-2.03$ echo $PATH
/export/home/me

It works!

Thank you PHV and chipperMDW
630111
 
If you want to change the PATH variable every time you login, you can save yourself typing . /path/to/script at each login by editing the .profile (or for bash use .bashrc) in your home directory (as hinted at by chipperMDW).

In the .profile at the bottom, add the lines:
PATH=$PATH:/export/home/me
export PATH
thus appending your directory to the PATH variable.

Or use the following to re-define your PATH variable (in .profile or .bashrc):
PATH=/export/home/me
export PATH

I hope that helps

Mike.
 
Thanks for that, Mike042. I should have explained myself better earlier on. I'm fully aware of ~/.profile, ~/.bashrc, and ~/.cshrc. This carrying over of environment variables is supposed to occur after the login.

At first, I thought my dba wanted to redefine environment variables from a script and have that carry over once the script was finished. Apparently I misunderstood, because now he's talling me he wants to carry environment variables from script to script.

630111
 
... to carry environment variables from script to script

what about this:
1)The first script creates a temp file containing all the new environment variables.
2)The second script sources this file.
hth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top