Skip to content

Instantly share code, notes, and snippets.

@muupan
Created February 27, 2014 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muupan/9255699 to your computer and use it in GitHub Desktop.
Save muupan/9255699 to your computer and use it in GitHub Desktop.
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