Skip to content

Instantly share code, notes, and snippets.

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