Linux shell scripting basic - changing directory in shell scripts

β
You may want to run a shell script that changes your current working directory to another directory for convenience.
For instance, if you frequently visit your documents directory (~/Documents) and want to navigate there quickly, you can write a Bash script to do so.
In this thread, I will show you how to do it with the cd command. Along the way, I will explain some of the complexities of how cd behaves.

A Common Problem
Let's begin with a script that navigates to the ~/Documents directory. I'll refer to it as
http://chdirectory.sh:

Breaking down our code:
β’ cd /home/linuxopsys/Documents - changes the current working directory to ~/Documents.
β’ pwd - prints the working directory.
β’ echo $$ - $$ is a Bash internal variable containing the Process ID (PID) of the shell that is executing your script.

Running our script
As we can see, running our script results in the expected output of /home/linuxopsys/Documents as well as the process ID of the shell.
Checking to see where we are now that the script has been run.
You can clearly see that our directory has not changed. We weren't expecting this because the current directory had not been changed to /home/linuxopsys/Documents. So, what could be the issue?
Let's take a look at the process ID of our shell:
We can clearly see that the process ID of the shell we're in (PID 4504) and the shell script (PID 4574) are totally different.
This is common behavior. The script is run in its own independent shell (subshell/childshell). At the end of the script, this separate shell exits, leaving the parent shell, which we are currently in and it's not affected.
The question now is, how do we solve this problem? So, continue reading.

Running scripts in parent shell
We can use the source command to allow our script to execute commands in the current shell (parent shell).
The source command executes commands within the current shell context rather than starting a new shell to do so. The dot operator is a shortcut alias for the source command (.).
Great! As a result, we can execute bash script in the current shell.
Let's try this:
The above snippet clearly shows that the script's PID is that of the parent shell that executed it, and our parent shell's current directory is now ~/Documents.
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.

Making Use of Bash Functions
It's a pain to write a Bash script for each directory. Instead, we could combine several Bash commands into a single script:
Now, if we source the file, we can use the script's functions in our current terminal.

Using Aliases
Using the built-in alias command, we can improve our Bash functions even further. Because it requires less typing, an alias is more convenient to use than a function.
Let's convert our functions into their alias variants:
When compared to the previous bash functions, we can see how concise this is. Furthermore, the alias can be used in the same manner as the functions.
That's it! In this thread, we've seen several ways to use the cd command from within Bash scripts.
To begin, we discovered that running a shell script initiates its own process. Finally, we looked at how functions and the alias command could help us improve our Bash scripts.
Thank you for making it this far & hopefully you found this thread helpful Feedback is really appreciated
Check us out
@Linuxopsys if you liked this thread!!
As we will be tweeting more about Linux, sysadmin and devops on a daily basis.