Logo 
Search:

Unix / Linux / Ubuntu Forum

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds

Changing directories in a script

  Date: Nov 30    Category: Unix / Linux / Ubuntu    Views: 388
  

This will change directories on the command line or in a script.

cd xyz

How do you change directories and stay in that directory after the script ends?

Share: 

 

7 Answers Found

 
Answer #1    Answered On: Nov 30    

You don't.

Once the script has ended, nothing more is being done by that process, so
it dies. A process that no longer exists can't have a current working
directory.

 
Answer #2    Answered On: Nov 30    

I'll repeat for those of you that did not understand the first time . . .

How do you change directories and stay in that directory after the script
ends?

A few years back I basically ask the same question and received an answer
that did work

While the script that dies no longer has a current directory the directory
it was in when the script ended is still there. How do I get the script to
leave the terminal session in the directory it had changed to? I don't know
what I'm saying wrong that you can't grasp that?

The reason I want to do this is I have a C code directory whose path is
quite long and want to create a script that can be executed that will take
terminal there, end and be in that directory after the script ends. The path
is something like ~/atmel/avr tools/avrstudio4/code/m8515/sw-led. So with a
script by the name of m8515sw-led.sh that took me to that directory and left
me there would sure help me from fat-fingering the path.

 
Answer #3    Answered On: Nov 30    

I am not aware of any clean way of doing what you want to do. Per the
bash documentation:

"A script can export variables only to child processes, that is, only to
commands or processes which that particular script initiates. A script
invoked from the command-line cannot export variables back to the
command-line environment. Child processes cannot export variables back
to the parent processes that spawned them."

You could write the $PWD of your script to a temp file (/tmp/whatever)
then cd to `cat /tmp/whatever` (note backward quotes which designate
"output of") when you exit.

 
Answer #4    Answered On: Nov 30    

You could create an alias in your path in the script you are executing
which would allow you to enter on the terminal command line something
like cd $program for instance and you would end up in your program
directory.
You can also put in your .profile so when you are logged in it is part
of your
path.
I am sure someone else will respond with this and other answers too.

 
Answer #5    Answered On: Nov 30    

If you source the script instead of executing it, it will remain in that
directory.

instead of "sh myscript.sh" or ./myscript.sh

do

. myscript.sh or source myscript.sh

 
Answer #6    Answered On: Nov 30    

That worked! It does just what I want it to do.

 
Answer #7    Answered On: Nov 30    

One thing you can do in a script or on the command line is to not use the cd
command to change directories, but use the pushd and popd commands to keep a
directory stack. You can even save environment variables that define directory
paths or even the current directory stack with the dirs -l command:
curdirstk=`dirs -l`

If you use cd in a script to change the directory from the current directory
from which the script is launched, after the script exits, the current directory
is the same as the one from which the script was launched - because the script
starts up a subprocess environment which has no choice in the matter but to
expire when done which cancels any directory changes and subprocess environment
thus leaving you back in the current directory from which the script was
launched.

 
Didn't find what you were looking for? Find more on Changing directories in a script Or get search suggestion and latest updates.




Tagged: