LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 08-28-2005, 12:29 PM   #1
ghrellin
Member
 
Registered: Dec 2004
Location: USA
Distribution: Gentoo Linux
Posts: 41

Rep: Reputation: 15
cd keeps snapping back to current directory when called from bash script


Here's the script:
Code:
#! /bin/bash -x

if [ -n $1 ]; then
        cd /home/gabe/$1/ && ls
else
         echo "Blah!"
fi
And here's what happens:
Code:
gabe @ tux ~ $ pwd
/home/gabe

gabe @ tux ~ $ ls amarok-svn/
MakeVars.in     README           config.h         configure.in      stamp-h1
Makefile        acinclude.m4     config.h.in      configure.in.bot  subdirs
Makefile.am     aclocal.m4       config.log       configure.in.in
Makefile.am.in  admin/           config.status*   doc/
Makefile.cvs    amarok/          configure*       libtool*
Makefile.in     autom4te.cache/  configure.files  stamp-h.in

gabe @ tux ~ $ script amarok-svn
+ '[' -n amarok-svn/ ']'
+ cd /home/gabe/amarok-svn/
+ ls
MakeVars.in     README          config.h         configure.in      stamp-h1
Makefile        acinclude.m4    config.h.in      configure.in.bot  subdirs
Makefile.am     aclocal.m4      config.log       configure.in.in
Makefile.am.in  admin           config.status    doc
Makefile.cvs    amarok          configure        libtool
Makefile.in     autom4te.cache  configure.files  stamp-h.in

gabe @ tux ~ $ pwd
/home/gabe
So the directory exists, it tells me that it knows it exists, AND it will cd to it in the script and tell me the contents. Yet it always snaps back to the directory it's in when I start the script.
 
Old 08-28-2005, 12:32 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
I'm not sure I see a problem - what are you wanting to happen?
 
Old 08-28-2005, 12:36 PM   #3
ghrellin
Member
 
Registered: Dec 2004
Location: USA
Distribution: Gentoo Linux
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks for the quick reply. And in answer to your question, I want it to stay in the directory I pass to the script.
 
Old 08-28-2005, 12:40 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
In that case you will need to source the commands into your current shell and not start a new one. Just run the script like:
. script amarok-svn
or:
source script amarok-svn
 
Old 08-28-2005, 12:46 PM   #5
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
Scripts are run in a subshell. Only the subshell changes directory.
It will work if you source the script: '. script'

I don't understand what you need this for, but you can create an alias in your .bashrc, like so: alias mycd='. script'
 
Old 08-28-2005, 12:50 PM   #6
ghrellin
Member
 
Registered: Dec 2004
Location: USA
Distribution: Gentoo Linux
Posts: 41

Original Poster
Rep: Reputation: 15
RESOLVED

Thanks for the tip. I decided to just make it a function in my .bashrc instead, since I use it a lot.
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 07:32 AM
setting bash to look in current directory before searching the path muhkuhmasta Linux - Newbie 4 09-21-2004 02:08 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
BASH: How to get current workin directory? gmitra Programming 9 09-20-2003 10:30 PM
Help creating a directory back up shell script WarriorWarren Linux - General 6 04-06-2003 09:56 AM


All times are GMT -5. The time now is 07:20 PM.

Main Menu
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