Skip to content

Instantly share code, notes, and snippets.

@jaydub
Last active October 9, 2023 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydub/5d5e87cd6a5654147c4715bb8f542cb4 to your computer and use it in GitHub Desktop.
Save jaydub/5d5e87cd6a5654147c4715bb8f542cb4 to your computer and use it in GitHub Desktop.
A $PATH de-duplicator for bash
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[*]}"
}
@jaydub
Copy link
Author

jaydub commented Oct 9, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment