Fedora

How to Add a Directory to the PATH in Fedora Linux

In this guide, we will learn about adding a directory to $PATH in Fedora.

Prerequisites:

To perform the steps that are demonstrated in this guide, you need the following components:

PATH in Fedora

Let’s start with a practical example. Run the following command from the terminal:

$ echo "hello world"

Here:

  • 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 echo
$ which which
$ which nonexistent

Listing the Directories in $PATH

To check the currently configured directories in $PATH, run the following command:

$ echo $PATH

For a more intuitive output format, use any of the following commands instead:

$ echo "${PATH//:/$'\n'}"

$ tr ':' '\n' <<< "$PATH"

$ sed 's/:/\n/g' <<< "$PATH"

$ xargs -n1 -d: <<< $PATH

$ echo $PATH | awk -F: '{for(i=1;i<=NF;i++)print $i}'

$ awk '{split($0,arr,":"); for(var in arr) print arr[var]}' <<< $PATH

$ python3 -c "print(*'${PATH}'.split(':'), sep='\n')"

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:

$ mkdir -pv /home/viktor/demo

Now, we place a Bash script in the directory for testing purposes:

$ nano test.sh

Enter the following code:

#!/bin/bash
echo "hello world"
echo "${PATH//:/$'\n'}"

Save the file and close the editor. Finally, mark the script as an executable:

$ chmod +x /home/viktor/demo/test.sh

Adding the Directory Temporarily to $PATH

To add the directory to $PATH for the current shell session only, run the following command:

$ export PATH="<path_to_dir>:$PATH"

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:

$ nano ~/.bashrc

Assuming there’s no previous entry for $PATH, add the following line at the end of the file:

$ export PATH="<path_to_dir>:$PATH "

Once updated, save the file and close the editor. To load the “bashrc”, restart the shell session or run the following command:

$ source ~/.bashrc

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:

$ cat /etc/environment

To include our custom directory in the $PATH system-wide, open the file in a text editor:

$ sudo nano /etc/environment

Now, update the value of $PATH as follows:

$ PATH=<path_to_dir>:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

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:

$ sudo nano /etc/security/pam_env.conf

There should already be an entry for the PATH environment variable. Uncomment the line and update the value as follows:

$ PATH           DEFAULT=${HOME}/bin:/usr/local/bin:/bin: :/usr/bin:/usr/local/bin/X11:/usr/bin/X11:<path_to_dir>

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:

$ echo "${PATH//:/$'\n'}"

For our demonstration, we put a Bash script in the directory. The “which” command should be able to report its location:

$ which test.sh

If $PATH is updated successfully, Bash should be able to execute the script without having to specify its exact location:

$ test.sh

If the steps are successful, the value of $PATH is successfully updated.

Conclusion

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.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.