My PATH Variable is a Mess
How to clean up your PATH in macOS/Linux
Introduction
When you install a program, it often asks you to add a PATH to your shell config file. I do it blindly thinking, “This tool is cool. I want to use it NOW!” After some time you find your PATH variable is a mess. A lot of duplication or triplication of PATHs. It is time to clean it up.
Check your PATH
The following command substitutes colons with new lines, sorts, counts the number of unique PATH occurrences, and displays:
$ echo $PATH | sed 's/:/\n/g' | sort | uniq -c
Do you have any PATH more than once? If so, read on!
macOS users
macOS has the path_helper
command. The path_helper
is a helper for constructing the PATH environment variable.
Run man path_helper
:
The default PATH and MANPATH values are in /etc/paths
and /etc/manpaths
. And also the path-helper
reads files in the etc/paths.d
and /etc/manpaths.d
directories.
Let’s check the content of the /etc/paths
:
$ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
These are the default paths on macOS. If you have these in your shell config file, it will repeat them in the PATH outputs, echo $PATH
.
Now let’s check the content of the /etc/manpaths
:
$ cat /etc/manpaths
/usr/share/man
/usr/local/share/man
When you use the man
command, it will search the above path for the man pages.
You can output PATH and MANPATH variables:
$ /usr/libexec/path_helper
You can check what files you have in etc/paths.d
and /etc/manpaths.d
.
$ ls /etc/paths.d
100-rvictl$ cat /etc/paths.d/100-rvictl
/Library/Apple/usr/bin$ ls /Library/Apple/usr/bin
rvictl
It loads the 100-rvictl
and it has /Library/Apple/urs/bin
. My /usr/libexec/path_helper
has /Library/Apple/usr/bin
in the first line.
Ubuntu users
Ubuntu uses /etc/environment
for the system-wide configuration and you find the default paths in this file.
ubuntu@shin-instance:/$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
ZSH users
If you are a ZSH user and there are too many paths in your ~/.zshrc
file, you can add the following at the end of your ~/.zshrc
:
# .zshrc
typeset -U PATH
This prevents duplicates of PATH variables.
Adding PATH and order of PATH
On UNIX, you can add multiple PATHs using colons:
PATH="/path1/to/file:/path2/to/file:/path3/to/file:$PATH"
If you want to prepend a path (before the default path) use:
PATH="/path/to/file:$PATH"
If you want to append a path (after the default path) use:
PATH="$PATH:/path/to/file"
For example, if you want to add the /usr/local/sbin
path:
export PATH="/usr/local/sbin:$PATH"
If your Bash scripts are in the ~/bin
directory and you want to prepend it:
export PATH="$HOME/bin:$PATH"
Don’t add $PATH
before and after the path you are adding to.
How to share PATHs in .zshrc, .bashrc, .bash_profile
You may want to share the same PATH in the ZSH and Bash shell. One way is to source the ~/.bashrc
in your ~/.zshrc
file.
# In the ~/.zshrc, sourcing ~/.bashrc
. ~/.bashrc
If you use iTerm2, iTerm2 has “Login Shell” under Preference > Profiles > General > Command. It will look for ~/.bash_profile
before ~/.bashrc
.
This means you need to source ~/.bashrc
in ~/bash_profile
:
# In ~/.bash_profile
. ~/.bashrc
Then in ~/.bashrc
, you add the PATH you want to use in the ZSH and Bash shell. Add PATH or source files that contain PATH.
# I have all the GNU/Linux PATH in ~/.macgnu file
. ~/.macgnu
export PATH="/path/to/file1:$PATH"# Terminal color
export PS1="\[\033[01;32m\]\w:$ \[\033[00m\]"# not display errors when using apropos and whatis
alias apropos="apropos 2>/dev/null"
alias whatis="whatis 2>/dev/null"
...
If you are sourcing other PATH files, it can be a cause of repeated PATH. You need to check carefully if you are sourcing the same PATH. Watch out not to add the same PATH in the different shell config files.
Conclusion
If you declare a PATH variable without appending nor prepending, you are not loading PATH from /etc/paths
. This will be a cause of the problem in a long run. You always append or prepend new paths to $PATH
variable in your config file, such as ~/.zshrc
, ~/bashrc
, etc. I recommend occasional PATH check-ups for a healthy Shell life. Writing common factors in the ~/.bashrc
and source it in the ~/.zshrc
is a good idea but be careful about duplication of PATHs. If you use ZSH, adding typeset -U PATH
prevents duplicates of PATH variables.