The PATH is an environment variable that is used to store the location of the resources that contain executable files on the Linux operating system. The PATH is defined using slash(/) and alphanumeric characters. Two types of PATH can be defined in Linux. One is an absolute path and the other is a relative path. The full location of a particular file or directory or file system is defined by an absolute path. The location that is relative to the current working directory is defined by the relative path. The $PATH variable mainly contains the location of those directories that execute frequently such as/bin, /usr/bin, /usr/local/bin, etc.
Different Examples of $PATH Variable
The different uses of the $PATH variable are shown in this part of the tutorial.
Example 1: Print the Current Value of the $PATH Variable
Create a Bash file with the following script that prints the current value of the $PATH variable using the “echo” and “printf” command:
printf "The current value of the PATH variable:\n"
#Print the values of the $PATH using 'printf' command
printf "%s" $PATH
#Add two newlines
printf "\n\n"
echo "The current value of the PATH variable: "
#Print the values of the $PATH using 'echo' command
echo $PATH
Output:
The following output appears after executing the previous script:
Example 2: Set the $PATH Variable Temporarily
The new path location can be appended temporarily or permanently in the $PATH variable. The newly added path location works until the terminal closes. The method of adding a new location in the $PATH variable temporarily is shown in this tutorial. The new path can be appended with the content of the $PATH variable using the “export” command in the terminal:
Run the following command from the terminal to print the current content of the $PATH variable:
The following output appears after executing the previous command:
Create a Bash file named path2.bash with the following script to append a new path location in the $PATH variable and print the value of the $PATH variable after adding a new path location.
printf "The current value of the PATH variable:\n"
#Print the values of the $PATH using 'printf' command
printf "%s" $PATH
Run the following command to execute the previous script:
The following output appears after executing the previous script. The newly added path is shown in the output:
Re-open the terminal and check the $PATH variable again.
The newly added path is not displayed in the following output because the path location was stored temporarily.
Example 3: Set the $PATH Variable Permanently Using ~/.bashrc
The method of adding a new location in the $PATH variable temporarily is shown in this tutorial. The new path can be appended with the content of the $PATH variable permanently by adding the “export” command of the previous example in the ~/.bashrc file.
Run the following command to open the ~/.bashrc file with the root privilege:
Add the following line at the end of the file to append the new path location to the $PATH variable.
Export PATH=$PATH:/home/fahmida/Desktop/bash.
Run the following command after saving the ~/.bashrc file to activate the changes done by the “export” command:
Create a Bash file named path3.bash in the “/home/fahmida/Desktop/bash” location with the following script to print the value of the $PATH variable after adding a new path location:
#Print the values of the $PATH using 'printf' command
printf "%s" $PATH
Run the following command to execute the previous script from the “/home/fahmida/” location where the path3.bash file does not exist:
The following output appears if the “/home/fahmida/Desktop/bash” location is appended properly in the $PATH variable. The newly added path is shown in the output:
Run the following command from the terminal to print the current content of the $PATH variable:
The newly added path is shown in the output because the new path location was added permanently.
Example 4: Set the $PATH Variable Permanently Using /etc/profile
The $PATH variable can be set permanently by configuring the /etc/profile file. If the path of the Bash script file is stored permanently in the /etc/profile file, the script can be executed from any location. There is no write permission for the /etc/profile file by default. So, the write permission for the /etc/profile file is required to be set before adding the command of the new path location.
Run the following command to add the write permission of /etc/profile file for all users:
Run the following command to open the /etc/profile file with the root privilege:
Add the following line to the end of the file:
Run the following command after saving the /etc/profile file to activate the changes done by the “export” command:
Create a Bash file named path4.bash in the “/home/fahmida/code” location with the following script to print the value of the $PATH variable after adding a new path location.
Run the following command to execute the previous script from the “/home/fahmida” location where the path4.bash file does not exist:
The following output appears if the “/home/fahmida/code” location is appended properly in the $PATH variable. The newly added path is shown in the output:
The /etc/environment file can be used also to set the $PATH variable permanently.
Conclusion
The methods of executing the script from any location by storing the script location path in the $PATH variable are shown in this tutorial. The value of the $PATH variable can be modified permanently or temporarily. Three different ways of appending a value to the $PATH variable are shown here by multiple examples that will help the Bash users to know the use of the $PATH variable properly.
from https://ift.tt/GFErP2J
0 Comments