Atlantic.Net Blog

How to Set $Path Variable in Linux

The $PATH variable plays an important role in Linux and Unix-based operating systems. It contains a list of directories that hold various executables on the system. The $PATH variable allows you to run any programs in Linux seamlessly. Generally, the $PATH variable contains the /bin, /usr/bin, /usr/local/bin, /sbin and /usr/sbin directories. However, you can also add your own directories to the $PATH variable to execute any script from anywhere on the system without specifying the script’s absolute path.

In this post, we will show you how to set a $PATH variable in Linux.

Prerequisites

  • A server running Linux on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing any Linux operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Check Current $PATH Variables

The $PATH variable is a colon-delimited list of directories that tell the Linux shell to determine where to search for an executable file.

To check the list of directories that currently exists in your $PATH, run the following command:

echo $PATH

You should see the following output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

You can also use the printenv command to list all variables:

printenv

Output:

LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
XAUTHORITY=/run/user/1000/gdm/Xauthority
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv

Also Read

How to Remove Files and Directories in Linux

Setting $PATH Variable Temporarily

If you just want to run any script in your current active session, then it is recommended to set a temporary $PATH variable. After setting up a $PATH variable, you can able to run your script with a command from anywhere on your system without specifying the full path of the script.

Use the following syntax to set a $PATH variable temporarily:

export PATH=$PATH:/directory-path

For example, if your script is located inside the /home/vyom/app directory, run the following command to add /home/vyom/app directory to a $PATH variable.

export PATH=$PATH:/home/vyom/app

This command will set a $PATH variable only for your active session. It will reset back to the default after the system restart.

Setting $PATH Variable Permanently

If you want to use any program or script regularly, it would be recommended to set a $PATH variable permanently. You can add a $PATH variable to ~/.bashrc and /etc/profile file.

  • If you want to set a $PATH for a specific user then you will need to add the $PATH variable inside the user’s ~/.bashrc file.
  • If you want to set a $PATH for all users then you will need to add the $PATH variable inside /etc/profile file.

For example, to add a $PATH variable for a specific user, edit the ~/.bashrc file:

nano /home/vyom/.bashrc

Add the following line:

export PATH=$PATH:/home/vyom/app

Save and close the file, then update the current shell variable using the following command:

source /home/vyom/.bashrc

To add a $PATH variable for all users, edit the /etc/profile file:

nano /etc/profile

Add the following line:

export PATH=$PATH:/home/vyom/app

Save and close the file, then update the current shell variable using the following command:

source /etc/profile

You can now check the added variable using the following command:

echo $PATH

Also Read

How to Compare Two Files in Linux Terminal

Conclusion

In this post, we explained how to set a $PATH variable in Linux. You can now add your desired directory to your user or global $PATH variable. Try it on VPS hosting from Atlantic.Net!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year