Here’s an intelligible one-liner solution that does all the right things: removes duplicates, preserves the ordering of paths, and doesn’t add a colon at the end. So it should give you a de-duplicated PATH that gives exactly the same behaviour as the original: PATH="$(perl -e 'print join(":", grep { not $seen{$_}++ } split(/:/, $ENV{PATH}))')" It simply splits on colon (split(/:/, $ENV{PATH})), uses uses grep { not $seen{$_}++ } to filter out any repeated instances of paths except for the first occurrence, and then joins the remaining ones back together separated by colons and prints the result (print join(":", ...)).