LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-05-2005, 02:35 PM   #1
jdupre
Member
 
Registered: Sep 2003
Distribution: LFS6.1, FC3, RH9
Posts: 36

Rep: Reputation: 15
How to change directory in a shell script?


I am trying to write a simple script that does some cleanup and then changes to a specific directory. But when the script finishes, I am returned to the directory that the script is in. How do I exit to the directory that I specified in the script???

#!/bin/bash
rm -Rf sourcecode
tar xvf sourcecode.tar
cd sourcecode
dir

When I run this script it prints the directory listing of sourcecode, but when the script exits I am left at the same directory that I started from. Is there a way to write a script that moves to a different directory or will it allways return to the directory that the script is in?

- Joe
 
Old 05-05-2005, 02:49 PM   #2
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
add this to the start of your script:
Code:
CWD=`pwd`
then add this to the end:
Code:
cd $CWD
 
Old 05-05-2005, 04:12 PM   #3
jdupre
Member
 
Registered: Sep 2003
Distribution: LFS6.1, FC3, RH9
Posts: 36

Original Poster
Rep: Reputation: 15
That does not work for me. It doesn't matter how I "cd" within the script. When the script exits, it always returns to the directory that was current when the script was executed.

- Joe
 
Old 05-05-2005, 04:15 PM   #4
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
my bad... i understood it the other way around... i thought you wanted to know how to get back into the directory the script was run in... sorry... but either way, once the script exits it will take you back to where it was started from... i'm not sure how to change that from within the script, but from outside the script you could simply do a:
Code:
./script.sh && cd sourcecode

Last edited by win32sux; 05-05-2005 at 04:26 PM.
 
Old 05-05-2005, 05:21 PM   #5
jdupre
Member
 
Registered: Sep 2003
Distribution: LFS6.1, FC3, RH9
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks.

I know "win32sux" (what of win16?), but with DOS and Win32 batch files this is a non issue. When the batch job exits, it exits to wherever it happens to be. It has no concept of returning to the directory that was current when the batch was run.

- Joe
 
Old 05-06-2005, 03:21 AM   #6
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Rep: Reputation: 30
May be you need to modify the script to leave a config file in your home dir. For example you make the script to make a ~/.myscriptconfig file. Then when the script exit it writes the 'pwd' to that ~/.myscriptconfig file. When the script is run again, it will first look for ~/.myscriptconfig file, read the line (which is the last directory the script was in) in the file, then 'cd' to that directory, then run the other parts of the script.
 
Old 05-06-2005, 03:16 PM   #7
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Quote:
but from outside the script you could simply do a:
Code:
./script.sh && cd sourcecode
This works too and has the advantage that the cd command with the (possibly complex) directory name is still in the script so you don't need to remember and type it.
Code:
source ./script.sh
 
Old 05-06-2005, 04:47 PM   #8
jdupre
Member
 
Registered: Sep 2003
Distribution: LFS6.1, FC3, RH9
Posts: 36

Original Poster
Rep: Reputation: 15
Code:
source ./script.sh
Ah, that's what I was looking for. I was looking for a way to avoid having to type
cd some-really-long-name-1.1.23-patch-20050505
all the time.

Thanks.
 
Old 11-25-2010, 03:41 PM   #9
asmoscosa
LQ Newbie
 
Registered: Nov 2010
Posts: 1

Rep: Reputation: 0
Great! , this works well for me too.. Thanks jdupre. By the way, why does
it work?? how does the source command work? i tried to view its info but it
is not available, could you explain a bit?? Thanks again!
 
Old 01-28-2011, 01:37 PM   #10
davebritton
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
why source works

when you run a shell script it is running inside its own shell (its own process, started by your shell) and when it is done it it ends the process and you are back where you started. source sends the command in the script to your current shell process for execution within it, as if you were typing them, when it is done it just stays like it would if you had typed the script by hand. So use the source.... for cd's to long annoying directories.
 
Old 01-20-2012, 07:47 PM   #11
Valmiki
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Smile

davebritton, this was a very nice explanation, saved me time on reading the man pages for source!

Quote:
Originally Posted by davebritton View Post
when you run a shell script it is running inside its own shell (its own process, started by your shell) and when it is done it it ends the process and you are back where you started. source sends the command in the script to your current shell process for execution within it, as if you were typing them, when it is done it just stays like it would if you had typed the script by hand. So use the source.... for cd's to long annoying directories.
 
Old 01-21-2012, 09:01 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Instead of setting it up as an independent script, you can also define it as a shell function. Functions run in the current environment, and so can affect it in the same way as direct commands.

http://mywiki.wooledge.org/BashGuide...ands#Functions

Code:
cleanup(){
	rm -Rf sourcecode
	tar xvf sourcecode.tar
	cd sourcecode
	dir
}
Put it in your bashrc or other startup file if you want it to always be available. Otherwise you can put the function itself in a separate file, and source that directly whenever you want it to be available for use.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 01:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 02:49 AM
directory renaming shell script? Apostasy Linux - General 15 02-22-2005 07:50 AM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration