Register
It is currently Thu Oct 02, 2014 10:30 am

change directory command


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Fri Jul 09, 2010 6:58 pm   

Joined: Fri Jul 09, 2010 6:53 pm
Posts: 5
I am pretty new to bash scripting.

I would like it so when I change directories the system will respond with my old directory path, and then the new directory path.

$>my_cd /bin
going from '/home/user/cdey' to '/bin'
$>

I would also like to write a command script that will allow me to go back to where I came from.

$>go_back
going back to '/home/user/cdey'
$>


Top
 Profile  
 PostPosted: Fri Jul 09, 2010 8:33 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
Hi,

you can't do this within a script, because commands are executed in a subshell (the script).
But you can do functions:
Code:
function my_cd { echo "going from $PWD to $1"; cd $1; }
function go_back { echo "going back to $OLDPWD"; cd ~-; }
Put these two lines into your $HOME/.bashrc, and then source it. Done! you can call them


Last edited by Watael on Sat Jul 10, 2010 1:39 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Sat Jul 10, 2010 12:54 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Watael wrote:
Code:
function my_cd { echo "going from $PWD to $1; cd $1; }
function go_back { echo "going back to $OLDPWD"; cd ~-; }

Nice, I didn't know the 'cd ~-' one, only 'cd -'
You learn something new every day :)

edit: there's a " missing in the 'echo' of the first function
Code:
function my_cd { echo "going from $PWD to $1"; cd $1; }


Top
 Profile  
 PostPosted: Sat Jul 10, 2010 12:09 pm   

Joined: Fri Jul 09, 2010 6:53 pm
Posts: 5
Thank you for the help.

This was an assignment for an intro to linux class, and we use a linux server and access it with putty, so I don't have access to the root folder.

I used the vi editor and made a file with this text: echo "going from $PWD to $1"; cd $1;

The only problem is it doesn't change the directory.


Top
 Profile  
 PostPosted: Wed Sep 01, 2010 3:15 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
I guess we can't really expect you to read the comments left after you posted if you can't even read your text book...

::sigh::


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP