Last active
October 9, 2023 03:28
-
-
Save jaydub/5d5e87cd6a5654147c4715bb8f542cb4 to your computer and use it in GitHub Desktop.
A $PATH de-duplicator for bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deduplicate_path () { | |
IFS=':' read -r -a _path <<< "$PATH" | |
_path_uniq=() | |
for member in "${_path[@]}"; do | |
found=0 | |
for u_member in "${_path_uniq[@]}"; do | |
if [ "$member" = "$u_member" ]; then | |
found=1 | |
break; | |
fi | |
done | |
if ! [ "$found" -eq 1 ]; then | |
_path_uniq+=("$member") | |
fi | |
done | |
local IFS=":" | |
export PATH="${_path_uniq[*]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A lot of useful software uses the
eval "$(prog init bash)"
convention to manage your prompt, provide completions and do whatever. Altogether too frequently this code stuffs eg~/.local/bin
into your path for the umpteenth time. Drop this function into your.bashrc
and call it at the end to reduce that down to a more sensible set of paths.