Skip to content

What is the PATH Variable?

What the PATH Variable Does

The PATH variable stores a list of directories that contain executable commands. These directories are searched when a command is executed in a shell.

For example, the ls command is actually the name of an executable. So when ls is typed into the shell, the directory where the ls executable is stored is listed within the PATH variable.

To check all of the directories listed in the PATH variable, I ran the following command

console
echo $PATH

Running this command showed a long list of directories for me, some of which are actually from my Windows 10 PATH variable, but that is the result of me using WSL.

Echoed PATH variable

Each directory in the PATH variable is seperated by a colon.

Editing the PATH Variable

There are multiple ways to edit the PATH variable, and I'll list some here.

  1. Altering the system wide PATH variable in /root/etc/environment on Ubuntu
  2. Modifiying it in the current shell session
  3. Editing it for the current user in .bashrc
  4. Editing it for the current user in .bash_profile
  5. Etc (there are many other options)

I'll go over how I did options 2 and 3.

Editing the shell session PATH variable

To completely replace the PATH variable, the following command can be ran in a shell session

console
export PATH="<dir1>:<dir2>:<dir3>:<etc>"

To append entries to the PATH variable, the following command can be ran instead

console
export PATH="$PATH<dir1>:<dir2>:<dir3>:<etc>"

Editing the PATH variable using .bashrc

To edit the PATH variable using .bashrc, the .bashrc file can be opened in an editor, and the previous process can be copied by writing the commands in .bashrc rather than a shell session.