Skip to content

Instantly share code, notes, and snippets.

@dekobon
Created December 12, 2022 17:57
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 dekobon/590038b65d16d4472636c8c24c6d4e05 to your computer and use it in GitHub Desktop.
Save dekobon/590038b65d16d4472636c8c24c6d4e05 to your computer and use it in GitHub Desktop.
Example of different bash string tests
#!/usr/bin/env bash
# a)
unset FOO
if [ ! -z ${FOO+x} ]; then
echo "a) is true"
fi
# b)
FOO=""
if [ ! -z ${FOO+x} ]; then
echo "b) is true"
fi
# c)
FOO="BAR"
if [ ! -z ${FOO+x} ]; then
echo "c) is true"
fi
echo "###"
# d)
unset FOO
if [ -n ${FOO+x} ]; then
echo "d) is true"
fi
# e)
FOO=""
if [ -n ${FOO+x} ]; then
echo "e) is true"
fi
# f)
FOO="BAR"
if [ -n ${FOO+x} ]; then
echo "f) is true"
fi
echo "###"
# g)
unset FOO
if [[ -v FOO ]]; then
echo "g) is true"
fi
# h)
FOO=""
if [[ -v FOO ]]; then
echo "h) is true"
fi
# i)
FOO="BAR"
if [[ -v FOO ]]; then
echo "i) is true"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment