FUNDAMENTALS A Complete Guide for Beginners
The $PATH variable is one of the most important environment variables in Linux. It stores the path of the directories. When programmers give a command to execute a program, initially the program is searched inside all the directories enlisted inside the bash $PATH variable. This is a great help for the programmers because it enables them to execute a program from any directory. They can enlist the path of their frequently used program and run them from any location they desire.
Key Takeaways
- Learning about the $PATH variable.
- Learning how to add a new directory to the $PATH variable.
- Learning how to execute programs from different directories.
Free Downloads
What is the “$PATH” Variable in Linux?
The $PATH variable in Linux is basically an environment variable that specifies a list of directories where the system should look for executable files when you enter a command in the terminal without providing a full path to the executable. This allows you to run commands from anywhere in the terminal without having to specify the full path to the executable each time.
How to Set a “$PATH” Variable and Update?
If you keep your file in such a location that is not currently listed on your $PATH variable you can easily enlist that variable on the $PATH variable using the export command. Suppose I want to enlist the /home/susmit/script-location directory to the $PATH variable. For this execute the following command:
export PATH=/home/susmit/script-location:$PATH
Then I have to source ~/.profile to activate this new change. For this execute the below command:
source ~/.profile
The ~/.profile is a startup file located inside the user’s home directory. It contains all the PATH variable lists. After changing the PATH variable, this file needs to be updated using the source command.
Configuring the “$PATH” Variable Using a Bash Script
You can execute a program located in any directory if its location is enlisted on the $PATH variable. I have a script named myprogram.sh at /home/susmit/Desktop directory. And it has to be executable by all.
Script (myprogram.sh) >
#!/bin/bash
#printing a message on the terminal
echo "Hello from myprogram!"
Here, I will execute the myprogram.sh Bash script from the home directory, which is located inside the /home/susmit/Desktop folder. For this, I will create a new bash script named ‘script1.sh’, where I will add the /home/susmit/Desktop directory to the PATH variable. And, I will execute the previously created ‘myprogram.sh‘ program from this script (script1.sh). After that, I will also show how you can remove the added directory from the $PATH variable. To know more, follow the below steps:
Steps to Follow >
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano script1.sh
- nano: Opens the nano text editor.
- script1.sh: Bash script name.
❸ Copy the script mentioned below:
#!/bin/bash
#This script demonstrates the use of the $PATH variable
#Add a new directory to the PATH
new_path="/home/susmit/Desktop"
export PATH=$PATH:$new_path
echo "Current PATH: $PATH"
#Check if a program is in the PATH
program_to_check="myprogram.sh"
if command -v $program_to_check &>/dev/null; then
echo "$program_to_check is found in the PATH."
else
echo "$program_to_check is not found in the PATH."
fi
#Using a program from the newly added directory
echo "Running a program from $new_path:"
myprogram.sh
#Remove the added directory from the PATH
export PATH=${PATH//$new_path:/}
echo "Updated PATH: $PATH"
#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then the new_path=”/home/susmit/Desktop” command assigns the directory to the new_path variable. After that, the export PATH=$PATH:$new_path command has exported the new_path variable.
Afterwards, the echo “Current PATH: $PATH” command has printed the current value of the $PATH variable. After that the if command checks the existence of the myprogram.sh and prints a message depending on the existence or not. Afterward, the myprogram.sh bash script has been executed. Finally, the newly added directory has been removed from the $PATH variable and printed the current value of the $PATH variable.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❺ Use the following command to make the file executable:
chmod u+x script1.sh
- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- script1.sh: File that you want to make executable.
❻ Run the script by the following command:
./script1.sh
The image shows that the myprogram.sh Bash script has been executed from the user’s home directory. Then the PATH variable has been updated and the updated value has been printed on the terminal.
Conclusion
Learning the configuration of the Bash $PATH variable is an essential skill for any Linux user by enabling them to run any program from anywhere. It makes them an efficient programmer. This article has outlined the easiest and most effective methods for setting and modifying the $PATH variable, making readers skillful in executing their necessary commands from any directory whenever necessary.
People Also Ask
Related Articles
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial