Created
May 24, 2019 16:37
-
-
Save jefferys/b685fbb55eb70f46786f31aa004e189f to your computer and use it in GitHub Desktop.
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
# 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