Operating System - HP-UX
1832262 Members
2650 Online
110041 Solutions
New Discussion

modifying calling environment from ksh script

 
SOLVED
Go to solution
Ben Spencer
Occasional Advisor

modifying calling environment from ksh script

at the end of a ksh script I went to set the directory that the user will be in after the script ends.

I can cd to the directory but when the script ends it reverts to the calling environment. I could source the script rather than execute but that is not acceptable. The user will never remember to source it.

Is there any way I can affect the calling environment from within a ksh script?

Thanks.
henry, hwollman@moody.edu
5 REPLIES 5
Jesper Sivertsen
Frequent Advisor
Solution

Re: modifying calling environment from ksh script

You have to source the script to get it to work, but you could make an alias for the script like:
alias script='. script'

Good luck
Jesper
All in unix is files
f. halili
Trusted Contributor

Re: modifying calling environment from ksh script

Try to use a dot script.

Here's an example.
1) Create a script myfile ( in your home directory )
# vi myfile
root=/
cd $root
echo "hello"
2) Run the script
# . myscript
3) check your present dir
# pwd
/
4) You should be in the root directory now.

------ fnhalili
derekh
Ben Spencer
Occasional Advisor

Re: modifying calling environment from ksh script

Not what I wanted to hear, but if the only way to do it is the dot command then I am stuck.

Thanks.
Bruce Regittko_1
Esteemed Contributor

Re: modifying calling environment from ksh script

Hi,

Sorry, but Jesper is right. You will have to source the script. An alias is probably your best bet. Remember, if the user is running the C shell, the command is

source script

and not the "dot".

--Bruce
www.stratech.com/training
Rajeev Tyagi
Valued Contributor

Re: modifying calling environment from ksh script

When you execute a program from shell it forkes a new process and it can take the environment variables from parent but viceversa is not possible and that is why when you come out of the child shell you loose all setting for child shell. So the only way is to source it with a . command.

Eg. #. <Script-name>