If you have a huge $PATH, run this to split every item into new line

echo $PATH | tr : \\n

Sort the output so similar items are together and count them up with uniq -c

echo $PATH | tr : \\n | sort | uniq -c

Now recombine remove the counter and recombine (with paste) into a single string:

echo $PATH | tr : \\n | sort -r | uniq | paste -s -d :

Sidenotes:
– We do a reverse sort because it tends to put the more important paths at the top or at left hand side, which are the first paths to get explored.
– Paste (paste -s) by default combines new line separated strings with tabs. However, we want to join the strings with : in between so we add -d :.

Add the following to the bottom of your .bashrc or .bash_profile script.

# clean up PATH (comment out might have bad effect)
export PATH=$(echo $PATH | tr : \\n | sort -r | uniq | paste -s -d :)

Sidenote:
– I add in the comment just as a reminder that this might have a bad effect and we can remove or comment it out.

Leave a Reply

Your email address will not be published. Required fields are marked *