Skip to content

Instantly share code, notes, and snippets.

@Spaceghost
Created January 20, 2015 04:03
Show Gist options
  • Save Spaceghost/4a785d729ca481e33ef8 to your computer and use it in GitHub Desktop.
Save Spaceghost/4a785d729ca481e33ef8 to your computer and use it in GitHub Desktop.
Idempotence in your path.
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