How to change directory in Linux shell scripts
For convenience, there are times you may want to run a shell script that changes your current working directory to another directory. For example, if you frequently visit your projects directory(~/Projects) and want to quickly navigate there, you can write a Bash script to do so.
In this guide, I’ll explain how you can change directory in Linux shell scripts using the cd command. I will also explain some complexities of how cd behaves along the way.
A Common Problem
Let’s start with a script that navigates to the ~/Projects directory. We’ll call it chdir.sh:
#!/usr/bin/bash
# Filename: chdir.sh
# change our current directory to ~/Projects
cd /home/rinku/Projects
#print working directory
pwd
# print the PID of the shell running our script
echo $$
Let’s understand our code:
- “cd ~/Projects” — allows you to change your current working directory to ~/Projects.
- pwd — this command prints the working directory.
- echo $$− prints the PID of the shell running our script
The “$$” is a Bash internal variable that contains the Process ID (PID) of the shell running your script.
Running the code
[rinku@rinku][$] bash chdir.sh
/home/rinku/Projects
118922
[rinku@rinku][$]
As we can see, running our script produces the expected output of “/home/traw/Projects” as well as the shell’s process ID.
Let’s see what directory we’re in now that we’ve run the script.
[rinku@rinku][$] bash chdir.sh
/home/rinku/Projects
118922
[rinku@rinku][$] pwd
/home/rinku
[rinku@rinkue][$]
This is not what we expected because the current directory has not been changed to /home/traw/Projects. So, what may be the problem?
Let’s look at our shells’ process ID:
[rinku@rinku][$] echo $$
77714
[rinku@rinku][$]
We can clearly see that the process ID of the shell we’re in (PID 77714) and the shell script (PID 118922) are totally different.
This is normal behavior. The script is executed in a separated, independent shell (subshell/child shell). This separate shell exits at the end of the script, leaving the parent shell, which we are currently in, unaffected.
Now the question is, how can we overcome this problem? Well, continue to read
Running Scripts in Parent Shell
To allow our script to execute commands in the current shell (parent shell) we can use the source command. The source command executes commands within the current shell context instead of creating a new shell to execute them. The source command has a shortcut alias, called the dot operator (.).
Great! So we can run the bash script in the current shell: Let’s try out:
[rinku@rinku][$] source chdir.sh
/home/rinku/Projects
77714
[rinku@rinku][$]
Let’s also verify that the directory we’re in has now changed:
[rinku@rinku][$]
pwd
/home/rinku/Projects
[rinku@rinku][$]
Nice! So far, we’ve shown that we can run shell scripts in the current shell by using source.
Alternatively, we could have used the short form. operator.
[rinku@rinku][$] . chdir.sh
/home/rinku/Projects
77714
[rinku@rinku][$]
Using Bash Functions
Creating a Bash script for each directory is a pain. Instead, we could incorporate several Bash functions into a single script:
#!/usr/bin/bash
[rinku@rinku][$] echo $$
77714
[rinku@rinku][$]
# Filename: chdirs.sh
# This file should be sourcedfunction music() {
cd "~/Music"
}function docs() {
cd "~/Documents"
}
Now, if we source the file:
We can use the functions within that script in our current terminal:
[rinku@rinku][$] source chdirs.sh
[rinku@rinku] - [~][$] docs
[rinku@rinku] - [~/Documents][$] music
[rinku@rinku] - [~/Music][$]
Using Alias
We can improve our Bash functions even further by using the built-in alias command. An alias is easier to use than a function because it requires less typing.
Let’s see if we can convert our functions into their alias variants:
#!/usr/bin/bash
# Filename: chdirs.sh# This file should be sourced
alias music="cd ~/Music"
alias docs="cd ~/Documents"
In comparison to the bash functions we wrote previously, we can see how concise this is. Furthermore, we can use the alias in the same way that we have used the functions.
Conclusion
In this article, we’ve seen several ways to use the cd command from within Bash. First, we discovered that running a shell script starts its own process. Finally, we looked at how we could improve our Bash scripts by using functions and the alias command.
Thank you for making it this far & hopefully you found this article helpful. Feedback is really appreciated.