Adding something to $PATH?

This is more a general question and not directly Manjaro-related but I've often read "Add XY to your $PATH" or something similar and I never know what that actually means. Is it about a file I have to edit? Or is it about a command in the terminal I have to enter? What does adding something to $PATH actually do?

The example in question is from here:

Path and Environment
For quick access, add scala and scalac to your path. For example:
Environment Variable Value (example)
Unix $SCALA_HOME /usr/local/share/scala

(Btw does the editor in this forum allow something like tabs for formatting?)

1 Like

Your $PATH tells the shell where to look for programs when you run them without a full path.

You can always run, for example, /usr/bin/true, but if your $PATH contains /usr/bin you can run the command with just true.

Directories further up the list are prioritised, so when you have e.g. /usr/bin:/usr/local/bin, /usr/bin/ will be checked before /usr/local/bin/

Adding a directory to your $PATH is a case of exporting that variable to your shell.

In the current shell, instead of

$ /path/to/new/directory/runthething

you can do:

$ export PATH="$PATH:/path/to/new/directory"
$ runthething

The first line sets the variable to the current path plus the new path, the second line runs the command.

To set your $PATH permanently you can edit your shell's configuration file. For BASH that's normally ~/.bashrc or ~/.bash_profile. For ZSH it's normally ~/.zshenv.


You usually add something to your PATH environment variable by editing the ~/.bashrc file. Which is a text file located in your home directory and easily editable with any text editor.

The PATH environment variable is a string with colons delimiting each path. Your system will read that string left to right and stop when it finds a working path. So imagine that you have two executable scripts. One in ~/.local/share/bin and another in /usr/bin. if your PATH environment variable is set to ~/.local/share/bin:/usr/bin (not the colon delimiter), then when you type in your console it will execute the one in .local/share/bin. To execute the other, you would have to give its full path instead of just the file name.

When you are told to add something to the path, the general procedure is to edit ~/.bashrc. Inside you will want to add a line that will read like one of the two following options:

export PATH="path_to_add:$PATH"


export PATH="$PATH:path_to_add"

The first option will prepend your new path to the PATH environment variable, whereas the second option will append to it. Usually the first option is preferable, so you are sure no file inside that new path is masked by other entries in the PATH. The second option is less common, rare even.

But all of the above is meant only if you want to make this change permanent. Sometimes you may only wish for the PATH to be set for the current terminal session. On that case, you would not edit the .bashrc file and instead just issue that same command on the command prompt of your terminal. When you close it, the change to PATH is lost.

Managing your PATH environment variable is one of your tasks as the system administrator of your computer.

Security advisor

But I need to call your attention to the fact it must never start or end with a colon.

export PATH="$PATH:path_to_add:" <--- THIS IS BAD!

The way the PATH environment variable works, means that if you end it with a colon, you are adding a so-called null directory. The null directory always takes the value of your current directory. This can have security implications. Imagine you moved into a directory you just unpacked from a download and type the ls command to list its files. However there's a malicious ls script file in there. That is what will be executed instead of the normal ls command.

Also note that there are 3 ways to create a null directory entry in the PATH environment variable:

  1. Start it with a colon: export PATH=":$PATH:path_to_add" <-- BAD
  2. End it with a colon: export PATH="$PATH:path_to_add:" <-- BAD
  3. Using two adjacent colons: export PATH="$PATH::path_to_add" <-- BAD

Nice, thanks guys. :slight_smile:
Tried it with Discord and it worked. In the current shell as well as when editing ~/.bashrc

This command didn't work for me, it said "not a valid identifier". Typing "export PATH="$PATH:/path/to/new/directory" did.

Ah, yeah, typo there. :slight_smile: Too many $.

I think you unintentionally have put a colon (:slight_smile: at the end of your last example.

AFAIK a colon at the end of an export statement causes whatever folder you're execute from to be included in the path - which AFAIK is or could be a security breach.

Please correct me if I am wrong.

1 Like

And a dangerous one at that. Many thanks for the correction!
I was drafting that post and incliuded the colon and a quick text exactly because I wanted to talk about this issue. I failed to realize I never finished that part and ended up giving the exact opposite advise.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Forum kindly sponsored by