cd works in shell but not in script

I frequently have to cd from $HOME to a particular long directory path. So I thought I’d put a cdscript in $HOME to make getting there a little quicker.


directory="/some/big/long directory path/that/I/use/frequently"
cd "$directory"

Set permissions:
chmod 700 cdscript

./cdscript doesn’t do anything. What am I missing? (Yes, those spaces in the path exist, and I can copy and paste the exact individual lines in the shell with success, so the path exists too). Also, is it more Unixey to just make a symbolic link to the directory instead of the above script, and cd to the link instead?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

just doing


won’t work. basically you forked a new shell, in which you cd, then the shell (and new working dir) exit.

You need to use

. ./cdscript

(there is a leading dot, and a space)

The first dot means : run ./cdscript as if I typed it.
The second dot is needed if . is not in your PATH var.

Solution 2

You would be better of creating an alias for this within your shell. For example in .bashrc, you could put;

alias cdscript='cd /really/long/file/path/'

Solution 3

The script changes your current working directory but then it is restored upon exit. Instead of typing


try typing

 . cdscript

to run your script for the desired result.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply