Last active
October 16, 2017 12:31
-
-
Save cbsmith/60d4f3754944bb1cc1fa to your computer and use it in GitHub Desktop.
An example on how to do idempotent PATH manipulation.
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
#!/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