Skip to content

Instantly share code, notes, and snippets.

@jefferys
Created May 24, 2019 16:37
Show Gist options
  • Save jefferys/b685fbb55eb70f46786f31aa004e189f to your computer and use it in GitHub Desktop.
Save jefferys/b685fbb55eb70f46786f31aa004e189f to your computer and use it in GitHub Desktop.
# Append potentially empty string, but with separator if not empty
# "one" -> "one" if "$extra" is empty, "one" -> "one, two" if not
result="one${extra:+", ${extra}"}"
# Examples
extra=;[ "one${extra:+", ${extra}"}" == 'one' ] || echo "FALSE"
extra=''; [ "one${extra:+", ${extra}"}" == 'one' ] || echo "FALSE"
extra='two'; [ "one${extra:+", ${extra}"}" == 'one, two' ] || echo "FALSE"
# Useful for path changes adding possibly empty dir to path
# or possibly prepending a dir to a possibly empty path.
PATH="$PATH${mayBeAddDir:+":${mayBeAddDir}"}"
PATH="$SOME_DIR${PATH:+":${PATH}"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment