Now, if you add the ~/Documents or /home/user/Documents to the PATH, you can access the “script.sh” by only executing its name without specifying the directory. Hence, adding the directory to the PATH can be a good idea because it helps you execute the files conveniently. In this short guide, we will explain the methods to add a directory to the PATH on Rocky Linux 9.
To add the directory to PATH, you must change the bashrc file. Let’s take an example to add the path of the “script.sh” which is located in the /Documents/My_Script directory. First, run the following command to open the bashrc in the terminal:
The given command opens the text editor. Add the following line at the end of the text:
Here, the “script.sh” is located in the /Documents/My_Script directory so we can execute the following command:
Once you are done, save the file and then run the following command to save the changes successfully:
The previous command executes the content of the bashrc file. Essentially, the source ~/.bashrc command is used to reload the bashrc to make the changes successfully. Furthermore, you can confirm the changes through the following command:
Now, let’s try to run the following command to execute the script in the terminal:
If you get the error, close the terminal and execute the command again. Moreover, you can run the following commands to open the script with no error:
If you want to remove the directory from the PATH, open the bashrc file again and then remove the directory path:
Once you are done, save the file and then reload the changes through the following command:
This is how you can easily add the directory to the PATH on Rocky Linux 9 and execute the file from the terminal. We also explained a quick method to remove the directory from the bashrc file. Adding a directory to the PATH helps you run the scripts or programs without specifying their path. Hence, you can use this method to save up your efforts of specify the path of the programs before executing them.