The Unix PATH is an environment variable that tells the shell which directories it should look for when you issue commands. You can think of it like a list of Application or Program Files folders.
What Does The PATH Do?
If you wanted to run a binary in your current folder, you’d have to provide a relative path, like so:
But, if you move that binary to a folder like
/usr/local/bin/, you instead simply type the command name to run it:
This is because
/usr/local/bin/ is on the PATH. The shell knows to look in that folder if you don’t specify a location. Many command line utilities are installed here, and in similar folders. You can view which folders are on your PATH by echoing the variable itself, using the
By default though, the PATH variable is a list of directory names split by colons. It’s not very readable:
But, with a bit of regex, you can clean it up to print out a list:
echo $PATH | sed $'s/:/\n/g'
These directories all serve different purposes:
- Folders ending in
binare general purpose, folders ending in
sbinare intended for system administration commands, things the system runs but you probably won’t interact with much. Though there are
/sbinare intended for programs that the system needs before
/usris available. This is where key programs like
/usr/sbinare for normal user programs, usually built in to the OS.
/usr/local/sbinare for programs you’ve downloaded or compiled yourself. If you have a binary you downloaded, and don’t know where to put it, chuck it in
/usr/local/sbin may not even exist on your system, as it’s fairly contradictory in nature. It isn’t present on macOS, but contains a few programs on Ubuntu.
You can view which folders different commands are located in with the
Which will print out the full path leading to the given command.
How To Add Folders To The PATH
Adding a folder to the PATH is simple, you just have to set the variable. But you can’t overwrite it completely, so you instead add a folder to the front of the PATH:
You can also tack the folder onto the back of the PATH:
These have different uses. Folders close to the front will be checked first, so if there are duplicate commands (for example, two versions of Python, both called “
python“) adding the folder to the front will ensure the folder you added manually will be checked first. Most of the time, this is probably what you want.
Usually, you’ll define this in your shell’s profile, which will set the variable whenever you open a new terminal. If you’re using bash, this is either
.bashrc. If you’re using a shell like zsh, this will be a different file (
.zshrc in this case). If there’s already a definition, you can simply add your folder to the front of the list, separated by a colon.
Consider Using Symlinks
Rather than adding a folder directly, you can instead use a symlink to specific commands, and place the symlink in
/usr/local/bin. For example, Python installs to its own folder, but rather than adding that folder to the PATH, it uses a symlink. You can view where symlinks lead with
ls -la $(which python)
And you can create symlinks with
ln -s command /usr/local/bin/command
The PATH Can Change
Since the PATH variable is usually defined in
.bashrc, your PATH may be different when running in a different environment. This is particularly relevant if you’re running inside a bash script, or if you’re running a command via
cron. If you’re having “command not found” issues, you’ll want to double check that the PATH is set correctly in the environment you’re running in.
Denial of responsibility! Swiftheadline is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – firstname.lastname@example.org. The content will be deleted within 24 hours.