on Tue, Jan 01, 2002 at 06:48:40PM -0800, Craig Dickson (crdic@yahoo.com) wrote:
> Jijo Jose A wrote:
>
> > I had a bash script and it have the line
> >
> > #--------- script begins
> > cd /usr/share/doc
> > pwd
> > exit 0
> > #---------- ends
> >
> > when i run the code within HOME
> > it outputs
> >
> > /usr/share/doc
> >
> > but after i exited from the script ,current directory remains the HOME.
> > i need to cd through the script .what can i solve this ?
>
> Your script is executed in a subshell, so of course your main shell
> isn't affected by the change of directory.
Yes. Understanding the issues involved in GNU/Linux shells, processes,
environments, and inheritance, is key.
What the original shell essentially does is:
I give birth to a child.
I tell the child to go down the street.
I tell the child to say were it is.
I kill my child (who says CompSci ain't brutal?).
I look where I am.
...surprise -- you're not in the place your child was. But you never
went there.
> You could simply source the script instead of executing it in a
> subshell, but I think a better solution would be to make this an
> alias.
I'd use a function. More flexible, essentially possible to do anything
you can do in a shell script. Though exiting is not generally
recommended:
$ function foo () { cd /usr/share/doc; pwd; }
$ foo
/usr/share/doc
$ pwd
/usr/share/doc
The function works on the current process. No birthing of children
involved (and less blood on your hands afterward).
Of course, there is a cost -- you're burdening your process with
remembering the function (it's stored in the processes environment).
$ typeset -f foo
foo ()
{
cd /usr/share/doc;
pwd
}
To free yourself:
$ unset foo
Peace.
--
Karsten M. Self <kmself@ix.netcom.com> http://kmself.home.netcom.com/
What part of "Gestalt" don't you understand? Home of the brave
http://gestalt-system.sourceforge.net/ Land of the free
We freed Dmitry! Boycott Adobe! Repeal the DMCA! http://www.freesklyarov.org
Geek for Hire http://kmself.home.netcom.com/resume.html
Attachment:
pgpC8vg9TTzXj.pgp
Description: PGP signature