Last active
April 6, 2019 06:49
-
-
Save michaelpass/c6906f3651c94bb879c1af4f6aee92c7 to your computer and use it in GitHub Desktop.
Deduplicae $PATH in .rc file
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 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