Pathcomp is a minimal tool for compressing a $PATH
-like environment
variables so that:
- Individual paths occur only once
- The (effective) ordering of paths remains unchanged
$ git clone [email protected]:mjhanninen/pathcomp.git
$ cd pathcomp
$ cargo install --path .
Typically you would have something like in your .profile
:
export PATH=$(pathcomp "$PATH")
In case you are unsure if pathcomp
is available:
if which pathcomp &>/dev/null; then
PATH=$(pathcomp "$PATH")
fi
export PATH
Pathcomp deduplicates paths while retaining the order of the first path instances:
$ echo $(pathcomp "/foo:/bar:/foo:/bar")
/foo:/bar
$ echo $(pathcomp "/bar:/foo:/bar:/foo")
/bar:/foo
Pathcomp pulls the paths matching the prefixes specified
with the --prefix
(or -p
) options to the front. If case multiple prefixes
match the same path then the prefix with longest match is used:
$ echo $(pathcomp -p /foo "/foo:/bar:/foo/bar/baz:/foo/bar")
/foo:/foo/bar/baz:/foo/bar:/bar
$ echo $(pathcomp -p /foo/bar -p /foo "/foo:/bar:/foo/bar/baz:/foo/bar")
/foo/bar/baz:/foo/bar:/foo:/bar