How to Change the PATH Variable in Amazon Linux

In Amazon Linux 2, as well as Linux in general, the PATH variable determines where the OS looks for executable files. This variable can be modified and allows for flexibility in organizing your directory structure. For example, let’s say that your OS is based off an image and all executable files that are installed with the image are located at the path, /bin. A user could then install executable files at the path of, /home/[username]/bin. And, the user could then modify the PATH variable, so that the executable files could run at the command line while only referencing the file name.

Photo by Lili Popper on Unsplash

Understanding the PATH Variable

The PATH variable is a colon-separated list of directories. When you execute a command, the system searches for the corresponding executable file in these directories from left to right. If it finds a matching file, it executes the command. To view your current PATH variable, open a terminal and run the following command:

$ echo $PATH

Temporarily Changing the PATH

If you want to modify the PATH variable temporarily for the current session, you can use the export command. For example, to add a directory to the PATH:

$ export PATH=$PATH:/path/to/your/directory

This change will only be effective for the current terminal session. Once you close the terminal, it will revert to its original state.

Permanently Changing the PATH

To make changes to the PATH variable that persists across sessions and system reboots for the current user, you need to modify the user or system profile files.

For a user-specific permanent change, you can edit the ~/.bashrc file using a text editor like nano or vim. Add the following line to the file:

export PATH=$PATH:/path/to/your/directory

Save the file and reload the profile:

$ source ~/.bashrc

Verifying the Changes

To verify that your changes to the PATH variable have taken effect, open a new terminal session and run:

echo $PATH

You should see the updated PATH variable including the directory you added.

Summary

Whether you need to make temporary or permanent changes to your PATH variable, Linux provides flexibility in where you can store your binary files and how you reference them when they are executed.