Last active
October 16, 2017 12:31
An example on how to do idempotent PATH manipulation.
This file contains hidden or 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
#!/usr/local/bin/bash | |
#How to manipulate the path in an intelligent fashion with bash. Note: this doesn't work with Bourne shell. | |
function idempotent_path_add { | |
DIR="$1" | |
PREPEND=$2 | |
if [[ ! "$PATH" =~ (^|:)"$DIR"(:|$) ]] | |
then | |
if [ $PREPEND ] | |
then | |
PATH="$DIR:$PATH" | |
else | |
PATH="$PATH:$DIR" | |
fi | |
fi | |
} | |
#examples: | |
#Add /usr/local/bin to the end of the path | |
# idempotent_path_add "/usr/local/bin" | |
#Add /sbin to the beginning of the path | |
# idempotent_path_add "/sbin" 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment