Created
January 20, 2015 04:03
-
-
Save Spaceghost/4a785d729ca481e33ef8 to your computer and use it in GitHub Desktop.
Idempotence in your path.
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
idempotent_path_prepend() { # idempotent_path_append dir1 dir2 dir3 => dir1:dir2:dir3:$PATH | |
for ((i=$#; i>0; i--)); do ARG=${!i} | |
if [ -d "$ARG" ]; then # Only add to path if the path currently exists. Might remove. | |
PATH=${PATH//":$ARG"/} # delete any instances in the middle or at the end | |
PATH=${PATH//"$ARG:"/} # delete any instances at the beginning | |
export PATH="$ARG:$PATH" # prepend to beginning | |
fi | |
done | |
} | |
idempotent_path_append() { # idempotent_path_append dir1 dir2 dir3 => $PATH:dir1:dir2:dir3 | |
for ((i=$#; i>0; i--)); do ARG=${!i} | |
if [ -d "$ARG" ]; then | |
PATH=${PATH//":$ARG"/} # delete any instances in the middle or at the end | |
PATH=${PATH//"$ARG:"/} # delete any instances at the beginning | |
export PATH="$PATH:$ARG" # append to end | |
fi | |
done | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment