71

I just want to write a script which changes my directory.

I put the below commands in the file /home/alex/pathABC

#!/bin/sh
cd /home/alex/Documents/A/B/C
echo HelloWorld

I did

chmod +x pathABC

In the Terminal, while in /home/alex, I ran ./pathABC, but the output is just HelloWorld and the current directory is not changed.

So what is wrong?

2
  • 9
    Look at this Why doesn't “cd” work in a bash shell script?
    – TuKsn
    Commented Jun 11, 2014 at 8:45
  • 'cd' works in a shell script. For example, you might have placed any script onto /home/alex/Documents/A/B/C path and you write shell script like: first it will go to the directory(via cd) then run the script(via ./) then it will. However, after that control will return back to the current path from where you are running ./pathABC script. The same case is happening with your script, it's going to the path and then once execution exits ,it return back to the location from where it's been called.
    – Shiwangini
    Commented Mar 28, 2020 at 13:29

8 Answers 8

107

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)

5. cd-able vars

Set the cdable_vars option in your ~/.bashrc and create an environment variable to the directory:

shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"

Then you can use cd pathABC

(from here)

5
  • 19
    Now I understand the usage of source! I always wondered why I do source .bashrc and not bash .bashrc
    – hytromo
    Commented Jun 11, 2014 at 12:05
  • 1
    Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
    – i2097i
    Commented Jan 28, 2017 at 20:13
  • 2
    > 5. cd-able vars -- isn't it cd $pathABC ?
    – loxaxs
    Commented Jul 2, 2017 at 9:56
  • 4th solution was more handy and useful at the moment
    – KJA
    Commented Jul 16, 2022 at 11:08
  • @loxaxs no, that's what cdable_vars does for you gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html.
    – Gauthier
    Commented Jul 18, 2022 at 15:40
12

When you run script in a Terminal, a child process runs. In this child program i.e. your script will change to whatever directory specified. But in the parent process, i.e. 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.

1
  • 2
    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
    Commented Jun 11, 2014 at 9:26
6

You are making a thinking error. While the current shell 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 running it from your script, after it has changed directory:

#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)

The second script would run from the new directory.

HelloWorld 

is just the output of the script.

2
  • 3
    HelloWorld is not "returned" to the parent shell, it's output to the standard output
    – Mog
    Commented Jun 11, 2014 at 12:01
  • Might be clearer if you just run pwd in the new directory, instead of adding a whole new script to the situation.
    – wjandrea
    Commented Jul 24, 2018 at 21:17
6

Actually, I just found, after many searches, that if you need to change the directory, and still keep the same shell, so you will get all the answers in your current script, you can use:

(cd your_dir; do_some_command_there)

For example, what I needed to use, was:

(cd your_dir; git remote -v | wc -l)

Works like a charm!

2

Use $SHELL at the end

A bash script operates on its current environment or on that of its children, but never on its parent environment.

However, this question often gets asked because one wants to be left at a command prompt in a certain directory after execution of a script from within another directory.

If this is the case, simply execute a child $SHELL instance at the end of the script:

# !/usr/bin/env bash
cd /home/alex/Documents/A/B/C
echo -e '\nHit [Ctrl]+[D] to exit this child shell.'
$SHELL

To return to the previous, parental shell instance, use Ctrl+D.

1

Trying to use cd inside the shell script does not work because the shell script runs in the subshell and once the script is over it returns to the parent shell, which is why the current directory does not change.

To achieve changing of the directory use sourcing.You can either use . scriptname.sh or source scriptname.sh command to use sourcing.

Note : Also when you use sourcing do not use the exit command because it then closes your connection.

0

Because hello world is just a trace statement, let's try this:

Create bash script file cd.sh containing:

#!/bin/bash
echo "/home/mike/Documents/A/B/C"
  • The .sh extension is an older convention of giving bash script filenames an extension. It's purely cosmetic and usually unnecessary. However in this case it's important to differentiate from the core cd command.

Mark the bash script file executable using:

chmod a+x cd.sh

Now run the file:

$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
  • cd we all know.
  • $(...) executes command inside parenthesis and returns output.
  • If cd.sh was in your path you don't need to specify where it is. We prefix with ./ to specify the command is in the current directory.
  • The echo output from the cd.sh script is sent back to the parent via the $(...). The parent (our shell prompt) uses this output and passes it to the Linux cd command.

As others have mentioned a child process can't change the parent's directory. This is one way the child can tell the parent where to go after the process ends.

3
  • @wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your 14.04 version I read about an hour ago or so? Commented Jul 24, 2018 at 22:51
  • Well, you totally changed the script. Previously it was just one command: cd /home/mike/Documents/A/B/C, which didn't produce any output. Now it's echo "/home/mike/Documents/A/B/C", which does produce output.
    – wjandrea
    Commented Jul 25, 2018 at 1:01
  • @wjandrea True, I also totally changed the way of calling the script too. Instead of a simple ./cd.sh it's now cd $(./cd.sh) but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting. Commented Jul 25, 2018 at 1:26
0

CDPATH might help in some cases.

# add this to .bashrc .zshrc or whatever
export CDPATH="/home/alex/Documents/A/B:$CDPATH"

# and then you can just do...
cd C

Note u have to add 'A/B' to CDPATH, not 'A/B/C' You can add multiple paths to CDPATH just like PATH.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .