Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a value to an environmental variable while avoiding putting an unnecessary colon.
# Add a value to an environmental variable while avoiding putting an unnecessary colon.
# Usage: add_env_with_colon PATH $HOME/local/bin
function add_env_with_colon {
ENV_VAR_NAME=$1
VALUE_TO_ADD=$2
CURRENT_VALUE=${!ENV_VAR_NAME}
if [ -n "$CURRENT_VALUE" ]; then
export $ENV_VAR_NAME=$VALUE_TO_ADD:$CURRENT_VALUE
else
export $ENV_VAR_NAME=$VALUE_TO_ADD
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment