How to Add a Directory to Your $PATH in Linux

Trending 1 week ago

Key Takeaways

  • Your PATH is simply a database of locations that your operating strategy checks erstwhile moving a command, allowing you to tally executables without manually specifying their nonstop location.
  • The ammunition searches for executables successful nan PATH successful a circumstantial order, starting pinch ammunition builtins, past searching done nan listed directories from near to right.
  • You tin adhd directories to your PATH utilizing nan export command, either temporarily aliases permanently by editing your .bashrc aliases .profile files. Just beryllium observant not to adhd a starring colon to debar information risks.

PATH is 1 of nan silent manipulators successful nan inheritance of your Linux computer. It softly affects your personification experience, but there's thing shady astir it. We'll explicate what it does, and really you tin set it.

What Is PATH connected Linux, and How Does It Work?

Your PATH is simply a database of locations that your operating strategy will cheque immoderate clip you effort to tally a command. If an executable that you effort to tally is contained successful a files that is included successful your PATH nan executable tin beryllium tally without specifying its nonstop location manually, since your operating strategy already knows wherever it is.

So really does it really activity functionally?

When you type a bid successful a terminal model and property Enter, you footwear disconnected rather a batch of activity earlier your bid is moreover executed.

Bash is nan default ammunition connected astir Linux distributions. It interprets nan statement of matter you entered and identifies nan bid names intermingled pinch nan parameters, pipes, redirections, and immoderate other is there. It past locates nan executable binaries for those commands and launches them pinch nan parameters you supplied.

The first measurement nan ammunition takes to find nan executable is identifying whether a binary is moreover involved. If nan bid you usage is wrong nan ammunition itself (a "shell builtin") nary further hunt is required.

Shell builtins are nan easiest to find because they're integral to nan shell. It's for illustration having them successful a toolbelt — they're ever pinch you.

If you request 1 of your different tools, though, you person to spell rummage successful nan shop to find it. Is it connected your workbench aliases a wall hanger? That's what nan PATH situation adaptable does. It holds a database of places nan ammunition searches and nan bid successful which they'll beryllium searched.

If you want to spot whether a bid is simply a shell builtin, an alias, a function, aliases a standalone binary mv /work/unfile, you tin usage nan type bid arsenic shown below:

type clear type cd
type clear successful a terminal window

This tells america that clear is simply a binary file, and nan first 1 recovered successful nan way is located astatine /usr/bin. You mightiness person much than 1 type of clear installed connected your computer, but this is nan 1 nan ammunition will effort to use.

Unsurprisingly, cd is simply a ammunition builtin.

How to List Your PATH

It's easy to spot what's successful your path. Just type nan pursuing to usage nan echo bid and people nan value held successful nan $PATH variable:

echo $PATH
echo $PATH successful a terminal widnow

The output is simply a database of colon (:) delimited record strategy locations. The ammunition searches from near to correct done nan path, checking each record strategy location for a matching executable to execute your command.

We tin prime our measurement done nan listing to spot nan record strategy locations that will beryllium searched, and nan bid successful which they will beryllium searched:

  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /bin
  • /usr/games
  • /usr/local/games
  • /snap/bin

Something that mightiness not beryllium instantly evident is nan hunt doesn't commencement successful nan existent moving directory. Rather, it useful its measurement done nan listed directories, and only nan listed directories.

If nan existent moving directory isn't successful your path, it won't beryllium searched. Also, if you person commands stored successful directories that aren't successful nan path, nan ammunition won't find them.

To show this, we created a mini programme called rf. When executed, rf prints nan sanction of nan directory from which it was launched successful nan terminal window. It's located successful /usr/local/bin. We besides person a newer type successful nan /dave/work directory.

We type nan pursuing which bid to show america which type of our program nan ammunition will find and use:

which rf
which rf successful a terminal window

The ammunition reports nan type it recovered is nan 1 successful nan directory that's successful nan path.

We type nan pursuing to occurrence it up:

rf successful a terminal window

Version 1.0 of rf runs and confirms our expectations were correct. The type recovered and executed is located successful /usr/local/bin.

To tally immoderate different type of rf connected this computer, we'll person to usage nan way to nan executable connected nan bid line, arsenic shown below:

./work/rf successful a terminal window

Now that we've told nan ammunition wherever to find nan type of rf we want to run, it uses type 1.1. If we for illustration this version, we tin transcript it into nan /usr/local/bin directory and overwrite nan aged one.

Let's opportunity we're processing a caller type of rf. We'll request to tally it often arsenic we create and trial it, but we don't want to transcript an unreleased improvement build into nan unrecorded environment.

Or, possibly we've downloaded a caller type of rf and want to do immoderate verification testing connected it earlier we make it publically available.

If we adhd our activity directory to nan path, we make nan ammunition find our version. And this alteration will only impact america — others will still usage nan type of rf successful /usr/local/bin .

Add a Directory to Your PATH

You tin usage nan export bid to add a directory to nan PATH. The directory is past included successful nan database of record strategy locations nan ammunition searches. When nan ammunition finds a matching executable, it stops searching, truthful you want to make judge it searches your directory first, earlier /usr/local/bin.

This is easy to do. For our example, we type nan pursuing to adhd our directory to nan commencement of nan way truthful it's nan first location searched:

export PATH=/home/dave/work:$PATH
export PATH=/home/dave/work:$PATH successful a terminal window

This bid sets $PATH to beryllium adjacent to nan directory we're adding, /home/dave/work, and past nan full existent path.

The first PATH has nary dollar motion ($). We group nan worth for PATH. The last $PATH has a dollar motion because we're referencing nan contents stored successful nan PATH variable. Also, statement nan colon (:) betwixt nan caller directory and nan $PATH adaptable name.

Let's spot what nan way looks for illustration now:

echo $PATH
echo $PATH successful a terminal window

Our /home/dave/work directory is added to nan commencement of nan path. The colon we provided separates it nan remainder of nan path.

We type nan pursuing to verify our type of rf is nan first 1 found:

which rf
which rf successful a terminal window

The impervious successful nan pudding is moving rf, arsenic shown below:

rf successful a terminal window

The ammunition finds Version 1.1 and executes it from /home/dave/work.

To adhd our directory to nan extremity of nan path, we conscionable move it to nan extremity of nan command, for illustration so:

export PATH=$PATH:/home/dave/work

How to Permanently Add Something to PATH

As Beth Brooke-Marciniak said, "Success is fine, but occurrence is fleeting." The infinitesimal you adjacent nan terminal window, immoderate changes you've made to nan $PATH are gone. To make them permanent, you person to put your export bid successful a configuration file.

When you put nan export bid successful your .bashrc file, it sets nan way each clip you unfastened a terminal window. Unlike SSH sessions, for which you person to log in, these are called "interactive" sessions.

In nan past, you would put nan export bid successful your .profile record to group nan way for log successful terminal sessions.

However, we recovered that if we put nan export bid successful either nan .bashrc aliases .profile files, it correctly group nan way for some interactive and log successful terminal sessions. Your acquisition mightiness beryllium different. To grip each eventualities, we'll show you really to do it successful some files.

Use nan pursuing bid successful your /home directory to edit nan .bashrc file:

gedit .bashrc
gedit .bashrc successful a terminal window

The gedit editor opens pinch nan .bashrc record loaded.

The gedit editor pinch nan .bashrc record loaded successful one t

Scroll to nan bottommost of nan file, and past adhd nan pursuing export bid we utilized earlier:

export PATH=/home/dave/work:$PATH

Save nan file. Next, either adjacent and reopen nan terminal model aliases usage nan dot bid to publication nan .bashrc file, arsenic follows:

. .bashrcThen, type nan pursuing echo bid to cheque nan path:

echo $PATH
. .bashrc successful a terminal window

This adds nan /home/dave/work directory to nan commencement of nan path.

The process to adhd nan bid to nan .profile record is nan same. Type nan pursuing command:

gedit .profile
gedit .profile successful a terminal window

The gedit editor launches pinch nan .profile record loaded.

The gedit editor pinch nan .profile record loaded into it

Add nan export bid to nan bottommost of nan file, and past prevention it. Closing and opening a caller terminal model is insufficient to unit nan .profile record to beryllium reread. For nan caller settings to return effect, you must log retired and backmost successful aliases usage nan dot bid arsenic shown below:

. .profile

Setting nan Path for Everyone

To group nan way for everyone who uses nan system, you tin edit nan /etc/profile file.

You'll request to usage sudo, arsenic follows:

sudo gedit /etc/profile

When nan gedit editor launches, adhd nan export bid to nan bottommost of nan file.

The gedit editor pinch nan /etc/profile record loaded into it.

Save and adjacent nan file. The changes will return effect for others nan adjacent clip they log in.

A Note connected Security

Make judge you don't accidentally adhd a starring colon ":" to nan path, arsenic shown below.

echo $PATH successful a terminal window

If you do, this will hunt nan existent directory first, which introduces a information risk. Say you downloaded an archive record and unzipped it into a directory. You look astatine nan files and spot different zipped file. You telephone unzip erstwhile much to extract that archive.

If nan first archive contained an executable record called unzip that was a malicious executable, you'd accidentally occurrence up that 1 alternatively of nan existent unzip executable. This would hap because nan ammunition would look successful nan existent directory first.

So, ever beryllium observant erstwhile you type your export commands. Use echo $PATH to reappraisal them and make judge they are nan measurement you want them to be.

Source Tutorials