Operating System - HP-UX
1820879 Members
4954 Online
109628 Solutions
New Discussion юеВ

How to change the working dir for parent shell?

 
SOLVED
Go to solution
Jack_27
Advisor

How to change the working dir for parent shell?

Hi,

I just hope to change the current working dir after a script has been executed.
That is,
ls
chdir mygrep mymake mytest temp
My expection is as below:
chdir(NOT . chdir)


However I wrote the script "chdir" with:
. cd temp
or exec cd temp
It doesn't work well,the working dir still keep the initial one.
Maybe the trap could be helpful?

Thanks
Jack
8 REPLIES 8
Jerome Baron
Respected Contributor
Solution

Re: How to change the working dir for parent shell?

Hi Jack,

Follow this link and goto 2.8 chapter.
http://groups.google.com/groups?dq=&hl=fr&lr=&ie=UTF-8&threadm=unix-faq/faq/part2_1036576409%40rtfm.mit.edu&prev=/groups%3Fgroup%3Dcomp.unix.questions

You can found :
2.8) How do I {set an environment variable, change directory} inside
a program or shell script and have that change affect my
current shell?

In general, you can't, at least not without making special
arrangements. When a child process is created, it inherits a
copy of its parent's variables (and current directory). The
child can change these values all it wants but the changes won't
affect the parent shell, since the child is changing a copy of
the original data.

Some special arrangements are possible. Your child process could
write out the changed variables, if the parent was prepared to
read the output and interpret it as commands to set its own
variables.

Also, shells can arrange to run other shell scripts in the
context of the current shell, rather than in a child process, so
that changes will affect the original shell.

For instance, if you have a C shell script named "myscript":

cd /very/long/path
setenv PATH /something:/something-else

or the equivalent Bourne or Korn shell script

cd /very/long/path
PATH=/something:/something-else export PATH

and try to run "myscript" from your shell, your shell will fork
and run the shell script in a subprocess. The subprocess is also
running the shell; when it sees the "cd" command it changes *its*
current directory, and when it sees the "setenv" command it
changes *its* environment, but neither has any effect on the
current directory of the shell at which you're typing (your login
shell, let's say).

In order to get your login shell to execute the script (without
forking) you have to use the "." command (for the Bourne or Korn
shells) or the "source" command (for the C shell). I.e. you type

. myscript

to the Bourne or Korn shells, or

source myscript

to the C shell.

If all you are trying to do is change directory or set an
environment variable, it will probably be simpler to use a C
shell alias or Bourne/Korn shell function. See the "how do I get
the current directory into my prompt" section of this article for
some examples.

A much more detailed answer prepared by
xtm@telelogic.se (Thomas Michanek) can be found at
ftp.wg.omron.co.jp in /pub/unix-faq/docs/script-vs-env.


Regards,
Jerome
Leif Halvarsson_2
Honored Contributor

Re: How to change the working dir for parent shell?

Hi

Start the script as:

. chdir
or
. ./chdir

Jack_27
Advisor

Re: How to change the working dir for parent shell?

Hi,Jerome&Leif

As I put such script in the bin directory,I don't want other users have to use the command ". /users/tester/bin/chdir",and wish the only "chdir" command could set the current dir successfully.
That's the reason why I mention "change dir for PARENT shell".

Cheers
Jack
John Palmer
Honored Contributor

Re: How to change the working dir for parent shell?

The only way to make the parent shell change directory is for it to execute the 'cd' command. Because the child script runs as a separate process, it can't do this.

You could do something like:

cd $(chdir)

The chdir script runs and prints the name of the directory on standard output and the parent shell cd's to it.

If you give some more information about what you are trying to achieve, I'm sure that a solution can be found.

Regards,
John
Chuck J
Valued Contributor

Re: How to change the working dir for parent shell?

Jack

Script commands are execute in the shell specified in the script, once the script has finished it exits the shell it was using and returns to the current environment.

If want to keep the environment of the script you have to execute the scipt using a . before.

e.g.

[/]# . /tmp/bin/yourscript

or

[/tmp/bin] # . ./yourscript

Chuck J
Jack_27
Advisor

Re: How to change the working dir for parent shell?

Hi,John&Chuck

Yes,I know that the child shell is running under another separate process.
My initial intention with which the script was developed:
1.Check the current environment variable.(OK)
2.Create/or not a new directory with the infor above.(OK)
3.Change the current directory to the new one which was just made.
(Notice: the child shell has finished now,so the dir changing was not effectiv.The script user has to set his dir by hand.I just want to remove such unnecessary step for convenience.)

Any comments please let me know.
Cheers
Jack
Jack_27
Advisor

Re: How to change the working dir for parent shell?

Hi,John&Chuck

Yes,I know that the child shell is running in another separate process.
My initial intention with which the script was developed:
1.Check the current environment variables.(OK)
2.Create/or not a new directory with the infor above.(OK)
3.Change the current dir to the new one that was made by step 2.
(Notice: Since the child shell has finished,the dir changing was not effective.
Thus the script user has to do such action by hand now,and I just want to remove these unnecessary steps for the user's convenience.)

Any comments please let me know.
Cheers
Jack
Chuck J
Valued Contributor

Re: How to change the working dir for parent shell?

Ok, I just tested this for you. If you have a script like this:
--------------------
#!/usr/bin/sh

DIR1=/tmp

cd $DIR1
--------------------

Then at the command line do this:

[server:/home/chuckj]# . ./script
It will change the directory to /tmp upon exit:
[server:/tmp]#

but if in your script you use the absolute path to the cd command i.e.

/usr/bin/cd $DIR1

then execute from your commnand line, it won't change the directory.

If you want to do this, have users start a new shell before running the script:

[server:/home/user]# sh
[server:/home/user]# . /tmp/scripts/script