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?
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
then access the directory with:
2. Alias
Put an alias in your ~/.bashrc:
(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
.
orsource
) causes the script to be executed in the same shell instead of running in its own subshell.(from here and here)
5. cd-able vars
Set the
cdable_vars
option in your~/.bashrc
and create an environment variable to the directory:Then you can use
cd pathABC
(from here)
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:
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:
The second script would run from the new directory.
is just the output of the script.
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:
For example, what I needed to use, was:
Works like a charm!
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
orsource scriptname.sh
command to use sourcing.Note : Also when you use sourcing do not use the exit command because it then closes your connection.
Use
$SHELL
at the endHowever, 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:To return to the previous, parental shell instance, use Ctrl+D.
Because hello world is just a trace statement, let's try this:
Create bash script file
cd.sh
containing:.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 corecd
command.Mark the bash script file executable using:
Now run the file:
cd
we all know.$(...)
executes command inside parenthesis and returns output.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.echo
output from thecd.sh
script is sent back to the parent via the$(...)
. The parent (our shell prompt) uses this output and passes it to the Linuxcd
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.
CDPATH
might help in some cases.Note u have to add 'A/B' to CDPATH, not 'A/B/C' You can add multiple paths to CDPATH just like PATH.