DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

How to Remove Duplicates in $PATH zsh

It is so easy to do that in zsh:

typeset -U path
Enter fullscreen mode Exit fullscreen mode

That's all.

As you can imagine -U stands for 'unique'. From doc:

       -U     For  arrays  (but not for associative arrays), keep only the
              first occurrence of each duplicated value.  This may also be
              set for colon-separated special parameters like PATH or FIG‐
              NORE, etc.  This flag has a different meaning when used with
Enter fullscreen mode Exit fullscreen mode

Btw. just care it is not -u. This flag just converts the content to uppercase.

All OK!

Appendix

You may wonder why is path instead of PATH? In zsh they are different but related things. The $path array variable is tied to the $PATH scalar string variable. The $path helps to find external programs, i.e. anything not part of the shell, any command other than a builtin, function or alias. Any modification on $path or $PATH is reflected in the other.

If you check them, you will see:

$ echo $path
/usr/local/bin /usr/bin /bin /sbin

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/sbin
Enter fullscreen mode Exit fullscreen mode

For more details you can go to : A User's Guide to the Z-Shell -
Path

Top comments (0)