Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is wrong with this easy script? I just want to write an script which change my directory:

A. I put below commands on the file witch its name is pathABC on the /home/alex directory,

cd /home/alex/Documents/A/B/C
echo HelloWorld

B. also I did chmod +x pathABC , On the terminal when I am on the /home/alex directory, I run ./pathABC .

But the output is just HelloWorld and the current directory remains with no change. I mean my directory remains as /home/alex and not go to the /home/alex/Documents/A/B/C.

So where is wrong?

share|improve this question
Look at this Why doesn't “cd” work in a bash shell script? – TuKsn Jun 11 '14 at 8:45
up vote 47 down vote accepted

As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.

Several alternatives:

1. Symbolic link

Put a symlink in your home to the long path you want to easily access

$ ln -s /home/alex/Documents/A/B/C ~/pathABC

then access the directory with:

$ cd ~/pathABC

2. Alias

Put an alias in your ~/.bashrc:

alias pathABC="cd /home/alex/Documents/A/B/C"

(from here)

3. Function

Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.

(from here)

4. Avoid running as child

Source your script instead of running it. Sourcing (done by . or source) causes the script to be executed in the same shell instead of running in its own subshell.

$ . ./pathABC

(from here and here)

share|improve this answer
Now I understand the usage of source! I always wondered why I do source .bashrc and not bash .bashrc – hakermania Jun 11 '14 at 12:05

When you run script in a terminal, a child process runs. In this child program ie your script will change to whatever directory specified. But in the parent process ie where you run the script is still in the old path. OR simply we can say:

The scope of cd command is only for child process not parent

share|improve this answer
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either . pathABC or source pathABC. – zwets Jun 11 '14 at 9:26

You are making a thinking error. While the current terminal stays in the same directory, the script has moved to the new directory.

You could see that by creating another script in the new directory, and adding it to your script, after it has changed directory:

cd /home/alex/Documents/A/B/C&&./ (if it is executable)

The (second) script would run from the new directory.


is just the output of the script.

share|improve this answer
HelloWorld is not "returned" to the parent shell, it's output to the standard output – Mog Jun 11 '14 at 12:01

Your Answer


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.