Created
October 13, 2022 04:53
-
-
Save lambdalisue/d6aeb48312f7b068811dc09d60a1b21f to your computer and use it in GitHub Desktop.
Bash test difference (-z/-n)
This file contains hidden or 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
check() { | |
# -z: True when the string length is 0 | |
if [ -z $DUMMY ]; then | |
echo '-z $DUMMY' | |
fi | |
if [ -z "$DUMMY" ]; then | |
echo '-z "$DUMMY"' | |
fi | |
if [ -z "${DUMMY+xxx}" ]; then | |
echo '-z "${DUMMY+xxx}"' | |
fi | |
if [ -z "${DUMMY:+xxx}" ]; then | |
echo '-z "${DUMMY:+xxx}"' | |
fi | |
# -n: True when the string length is greater than 0 | |
if [ -n $DUMMY ]; then | |
echo '-n $DUMMY' | |
fi | |
if [ -n "$DUMMY" ]; then | |
echo '-n "$DUMMY"' | |
fi | |
if [ -n "${DUMMY+xxx}" ]; then | |
echo '-n "${DUMMY+xxx}"' | |
fi | |
if [ -n "${DUMMY:+xxx}" ]; then | |
echo '-n "${DUMMY:+xxx}"' | |
fi | |
} | |
echo | |
echo '# When $DUMMY is not defined' | |
unset DUMMY | |
check | |
echo | |
echo '# When $DUMMY is an empty string' | |
export DUMMY="" | |
check | |
echo | |
echo '# When $DUMMY is a non-empty string' | |
export DUMMY="foo" | |
check |
Author
lambdalisue
commented
Oct 13, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment