How to Change Current Directory Using cd, pwd in Bash Script?

In Linux, all the files are organized in a tree-like directory structure starting from the root directory. In order to navigate into the desired directory Linux offers the “cd” command. It changes the present working directory to the targeted one. On the other hand, the built-in “pwd” command is quite useful when the users want to get the path of the current directory where they are working. 

This guide explains both the “cd” and “pwd” commands through the terminal or the bash script:

  • Change the Current Directory Using cd, pwd 
  • Change Current Directory Using Function
  • Change Current Directory Using alias

How to Change the Current Directory Using cd, pwd in Bash Script?

The “cd” command provides a way to change the current working directory in the present shell through the terminal or the bash script. Let’s see how this task can be performed:

Step 1: Create a Script

Create/open a bash script in the “nano” text editor having the following content:

$ nano script.sh

In the “script”.sh:

#!/bin/bash

cd /home/itslinuxfoss/Downloads
  • The “#!/bin/bash” denotes the “Bash Shebang” which executes the “script.sh” in the bash shell.
  • The “cd” command follows the absolute path of the “Downloads” directory in which “itslinuxfoss” shows the user name of this directory:

Press “Ctrl+S” to save and “Ctrl+X” to exit the text editor 

Step 2: Make the Script Executable

The newly created bags script does not have the execute permissions by default. To do so, execute the “chmod” command followed by the “x(execute)” flag to make the “script.sh” executable:

$ sudo chmod +x script.sh

The “script.sh” is now executable.

Step 3: Execute the Script

Lastly, specify the name of the “script.sh” with “dot().” and “forward-slash(/)”to run it:

./script.sh

The “script.sh” is executed in the subshell and does not change the directory in the terminal. 

Use of “source” Command

The above output is an exceptional behaviour and can be handled through the “source” command. It is the built-in shell command that is used to read and execute the file/script content in the current shell environment:

$ source script.sh

The current working directory has been changed to “Downloads” instantly.

Alternative: Use “dot”

The user can also mention the “dot(.)” symbol with the “script.sh” instead of the “source” command for changing the current active directory:

$ . script.sh

The output of the “dot(.)” symbol is the same as the “source” command.

Display pwd

In order to show the “present working directory” in the terminal after changing the directory in the script use the “pwd” command in the “script.sh”:

#!/bin/bash
cd /home/itslinuxfoss/Downloads
pwd

Save the new changes and exit the editor.

Run the script.sh with the help of “dot(.)” symbol:

$ . script.sh

At this time the “script” shows the pwd as “.home/itslinuxfoss/Downloads” and then changes the directory in the current shell environment.

Alternative 1: Change Current Directory Using Function

The “cd” command can also be implemented in a function defined in the terminal apart from the bash script. Let’s see how it can be performed practically.

Step 1: Create a Function

First create a function named “chng-dir” having no argument in the parenthesis. It defines a single “cd” command following the absolute path of the “Pictures” directory:

$ function chng-dir() {
  cd /home/itslinuxfoss/Pictures
}

The “chng-dir” function has been created.

Step 2: Invoke Function

Call the newly created “chng-dir”  function in the terminal and see the output:

$ chng-dir

The “chng-dir” function has successfully executed and changed the present working directory as “Pictures”

Alternative 2: Change Current Directory Using alias

Another shortest method to use the “cd” command is creating an “alias”. The alias acts as a shortcut to perform the defined task to avoid typing the long commands. It can be created temporarily/ permanently. 

Step 1: Create an alias

Type the “alias” keyword to declare the “chng” alias having the “cd” command along with the complete path of desired directory i.e “Pictures”:

$ alias chng='cd /home/itslinuxfoss/Pictures'

The “chng” alias has been declared.

Step 2: Activate the alias

Specify the “alias” name “chng” as a command and it will show you the same output as the “cd” command:

$ chng

The present working directory has now changed to “Pictures”.

Conclusion

To change the dictionary in the current bash shell, execute the script having “cd” and “pwd” commands with the “dot(.)” symbol and the “source” command. Both perform the same task and change the present working directory via “cd” defined in the script and show its absolute path via “pwd” command line utility.

This guide has described how to change the current directory using “cd”,”pwd” in the bash script.