Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have this script

#!/bin/bash
cd /home/user/somedir
pwd

it works as expected, but I would like this script to transport me to /home/user/somedir, but I stay in the same dir.

How to write script that will transport me (in gnome-terminal) to /home/user/somedir?

share|improve this question
    
possible duplicate of Why doesn't "cd" work in a shell script? –  muru Dec 4 at 18:15

3 Answers 3

You need to source your script. If not it will be run in a separate subshell, changing the working directory of the subshell but not of the shell you run it in.

To source it :

. myfile.sh

or

source myfile.sh

You can read more here

share|improve this answer
    
I was just going to add that.... UAdadapter if you were running the shelll script like sh myfile.sh or ./myfile.sh it would run as danjil described. Either of danjil's options should work for you. –  itnet7 Dec 1 '11 at 12:21

It works perfectly for me:

#!/bin/bash
cd $HOME/somedir
pwd
sleep 10
share|improve this answer
5  
why sleep 10???? –  UAdapter Dec 1 '11 at 13:15
    
Must be night time :) –  David Thomas Nov 15 at 9:07

I written an article for coming out from directories back in an easier way using the alias. It is over here. In short you can use :

back 2

to come two step back from the current directory. It is also explained over there how to put the code in ~/.bashrc so that every new shell opened will automatically have this new alias command. You can add new command to go to specific directories by modifying the code by adding more if condition inside the code for the arguments. You can also pull the code from my git over here.

share|improve this answer
    
Don't tell us what is written elsewhere - give steps and explanation here if you want to give a good answer. –  guntbert Dec 4 at 19:57
    
I dont care about wasting time to get the points. I already written there. In my point everything is the same and I dont have any regret in this and for you! –  Arun Chettoor Dec 5 at 7:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.