Skip to content

Instantly share code, notes, and snippets.

@michaelpass
Last active April 6, 2019 06:49
Show Gist options
  • Save michaelpass/c6906f3651c94bb879c1af4f6aee92c7 to your computer and use it in GitHub Desktop.
Save michaelpass/c6906f3651c94bb879c1af4f6aee92c7 to your computer and use it in GitHub Desktop.
Deduplicae $PATH in .rc file
# _
# _______| |__ _ __ ___
# |_ / __| '_ \| '__/ __|
# _ / /\__ \ | | | | | (__
# (_)___|___/_| |_|_| \___|
# Deduplicate path variables
get_var () {
eval 'printf "%s\n" "${'"$1"'}"'
}
set_var () {
eval "$1=\"\$2\""
}
dedup_pathvar () {
pathvar_name="$1"
pathvar_value="$(get_var "$pathvar_name")"
deduped_path="$(perl -e 'print join(":",grep { not $seen{$_}++ } split(/:/, $ARGV[0]))' "$pathvar_value")"
set_var "$pathvar_name" "$deduped_path"
}
dedup_pathvar PATH
dedup_pathvar MANPATH
dedup_pathvar PYTHONPATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment