LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
Search this Thread
Old 12-21-2006, 03:32 PM   #1
ernesto_cgf
Member
 
Registered: Nov 2003
Location: Havana, Cuba
Distribution: Ubuntu 9.04 "Jaunty Jackalope"
Posts: 153

Rep: Reputation: 30
Shell scripts unable to permanently change directory after exit


I need to do a shell script which creates a new directory and changes into it before exiting. I want the shell to be positioned at the newly created directory after the script exits, but when I do the obvious

Code:
mkdir $1
cd $1
and run it, I get my prompt at the original directory.
I googled for this and I only got this, but I do not want to get no for an answer so easily.
 
Old 12-21-2006, 03:54 PM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 46
You can use a bash function:
Code:
pu@slackw:~$ mkcs() { mkdir $1 ; cd $1 ; }
pu@slackw:~$ mkcs ttdir
pu@slackw:~/ttdir$
 
Old 12-21-2006, 03:56 PM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
Part one. Explanation of impossibility.

Consider how script is executed. A child process is spawned, and it is bash. It reads the body of the script, and executes one command a time. Then it exits, and its current directory is of no more interest. Parent's working directory is unaffected. And it has to be unaffected. If it was not, any process having child processes would never know what is its working directory. It would mean that the notion of current directory is nonsense.

Part two. Ray of hope.

Let me guess. You use bash and want to automate some action. Then read 'man bash', and edit your .bashrc to add a line:
alias mdcd='mkdir "$1"; cd "$1";'
. Not putting " around $1 will one day bite you...
 
Old 12-21-2006, 04:58 PM   #4
ernesto_cgf
Member
 
Registered: Nov 2003
Location: Havana, Cuba
Distribution: Ubuntu 9.04 "Jaunty Jackalope"
Posts: 153

Original Poster
Rep: Reputation: 30
raskin:

I did the

Code:
alias mdcd='mkdir "$1"; cd "$1";'
thing and I got the following

Code:
ernesto@antares:~$ mdcd testdir
mkdir: cannot create directory `': No such file or directory
bash: testdir: command not found
I'm sure this alternative should work someway, but maybe you missed something? Did you try this?

uselpa:

I created your function and it works! I prefer the "mdcd" name though.

Anyway, thanks to you both for the tips. I have my [small] problem solved now. Thanks a lot.
 
Old 12-21-2006, 05:08 PM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
Oops, sorry, certainly it has to be function. Forgot that aliases do not take parameters and that makes them differ from functions.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change directory in a shell script? jdupre Linux - Newbie 11 01-21-2012 09:01 AM
unable to change root shell sandin Slackware 3 04-17-2005 09:33 AM
unable to change directory in man 10.1 official ronss Mandriva 2 01-16-2005 02:03 AM
shell scripts & change dir pit1516 Linux - Newbie 6 06-22-2004 11:42 AM
Unable to Change File/Directory Protection. rvijay Linux - Newbie 2 09-15-2003 09:23 AM


All times are GMT -5. The time now is 04: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