To perform the steps that are demonstrated in this guide, you need the following components:
- A properly-configured Fedora system. For testing and learning, you can create a Fedora VM using VirtualBox.
- Access to a non-root user with sudo privilege.
PATH in Fedora
Let’s start with a practical example. Run the following command from the terminal:
- The terminal window interfaces with the default shell app (Bash, in most cases).
- We send a string to Bash to parse and execute.
- After parsing, Bash calls the “echo” command and send the “hello world” string as the argument.
- The output is printed on the console screen.
How did Bash know where to look for the “echo” command? We didn’t manually specify the binary location of echo.
This is the job of the PATH environment variable. It stores the location of multiple directories that the shell looks into whenever searching for a command to run. Need to check if a command exists in the locations that are described by $PATH? Use the “which” command:
$ which which
$ which nonexistent
Listing the Directories in $PATH
To check the currently configured directories in $PATH, run the following command:
For a more intuitive output format, use any of the following commands instead:
Adding Directories to $PATH
The following section demonstrates how to add a new directory to the PATH environment variable. While there are multiple ways to achieve the same result, it’s strongly recommended to stick to Bash-specific methods.
Creating a Test Directory
We add a demo directory containing a test script to $PATH. This helps determine the success of the process.
Create the demo directory:
Now, we place a Bash script in the directory for testing purposes:
Enter the following code:
echo "hello world"
Save the file and close the editor. Finally, mark the script as an executable:
Adding the Directory Temporarily to $PATH
To add the directory to $PATH for the current shell session only, run the following command:
Once the shell session ends, $PATH resets to the default value.
Adding the Directory Permanently to $PATH Using Bashrc
Whenever a Bash session starts, it loads a couple of “bashrc” scripts before it accepts any user interaction. There are two primary locations for the “bashrc” files:
- /etc/bash.bashrc or /etc/bashrc: A global “bashrc” file that all Bash instances run in the beginning, irrespective of the user. Root permission is needed to modify its content.
- ~/.bashrc: A local “bashrc” It’s only applicable for Bash sessions that are initiated by the respective user. Root permission is not needed for the modification.
To make a permanent change to $PATH, we have to declare its updated value in the “bashrc” file.
- If you want to make the change for all users in the system, update the global “bashrc”
- If you want to make the change for the current user, update the local “bashrc”
- If you’re not certain which to choose, modifying the local “bashrc” is always a safer option.
With that out of the way, open the desired “bashrc” file in a text editor:
Assuming there’s no previous entry for $PATH, add the following line at the end of the file:
Once updated, save the file and close the editor. To load the “bashrc”, restart the shell session or run the following command:
Adding the Directory Permanently to $PATH Using /etc/environment
Similar to bash.bashrc, the /etc/environment is a system-wide configuration file. It can also be used to specify the value of $PATH manually. The file is owned by root, so you need a root privilege to modify it.
By default, it should contain the absolute value of $PATH:
To include our custom directory in the $PATH system-wide, open the file in a text editor:
Now, update the value of $PATH as follows:
To take the changes into effect, restart the shell session.
Adding the Directory Permanently to $PATH Using /etc/security/pam_env.conf
This is yet another system-wide configuration file that can control the value of $PATH. It sets the value of the environment variable for all the users in the system. If both /etc/environment and /etc/security/pam_env.conf contain the same environment variable, the /etc/security/pam_env.conf is prioritized.
It requires a root permission to edit the file. Notice that it’s a highly sensitive configuration file and a syntax error or a wrong configuration prevents any user from logging in.
Open the file in a text editor:
There should already be an entry for the PATH environment variable. Uncomment the line and update the value as follows:
Save the file and close the editor. To take the changes into effect, you have to restart the shell session.
Verifying the Change
Once you updated the value of $PATH one way or another, we can run a couple of commands to verify that it was successful.
First, check the value of $PATH again and confirm that the new directory is included:
For our demonstration, we put a Bash script in the directory. The “which” command should be able to report its location:
If $PATH is updated successfully, Bash should be able to execute the script without having to specify its exact location:
If the steps are successful, the value of $PATH is successfully updated.
We demonstrated the various ways of adding a directory to the PATH environment variable in Fedora. We also showcased how to verify if the process is successful.