Skip to content

Instantly share code, notes, and snippets.

@shoemoney
Created October 2, 2021 11:06
Show Gist options
  • Save shoemoney/d3e17e2ff757561d3cbe7fdc7855c136 to your computer and use it in GitHub Desktop.
Save shoemoney/d3e17e2ff757561d3cbe7fdc7855c136 to your computer and use it in GitHub Desktop.
Bash compare operators
Check if empty
if [ -z "$var" ]
then
echo "\$var is empty"
else
echo "\$var is NOT empty"
fi
OR
if test -z "$var"
then
echo "\$var is empty"
else
echo "\$var is NOT empty"
fi
Another option to check if bash shell variable is empty or not
You can also try the control operators. The syntax is:
[ -z "$var" ] && echo "Empty"
[ -z "$var" ] && echo "Empty" || echo "Not empty"
OR
[[ -z "$var" ]] && echo "Empty"
[[ -z "$var" ]] && echo "Empty" || echo "Not empty"
OR
## Check if $var is set using ! i.e. check if expr is false ##
[ ! -z "$var" ] || echo "Empty"
[ ! -z "$var" ] && echo "Not empty" || echo "Empty"
[[ ! -z "$var" ]] || echo "Empty"
[[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
For example:
## taken from gen_html_to_pdf() ##
IFS='|'
set -- $line
local pdfid="$1"
local pdfurl="$2"
local pdftitle="$3"
local pdf="${output}/${pdfid}.pdf"
[[ -z "$pdfurl" ]] && { echo "Error: URL not found in $_db"; exit 1; }
echo "Creating $pdf ..."
# rest of code ...
Sh/Bash one liner examples to determine if a bash variable is empty
Type the following command
## set _JAIL
_JAIL="/path/to/apache"
## find out if it is empty or not ##
[ -z "$_JAIL" ] && echo "Empty: Yes" || echo "Empty: No"
Another example:
_JAIL=""
[ -z "$_JAIL" ] && echo "Empty: Yes" || echo "Empty: No"
Another example:
## unset (remove) $_JAIL ##
unset _JAIL
[ -z "$_JAIL" ] && echo "Empty: Yes" || echo "Empty: No"
if command syntax and example
#!/bin/sh
_JAIL="$1"
if [ -z "$_JAIL" ]
then
echo "Please set \$_JAIL"
else
echo "Setting up jail at $_JAIL"
//call setjail()
// setjail
fi
A note about double bracket syntax
If portability is not your concern try:
## set _JAIL
_JAIL="/path/to/apache"
## find out if it is empty or not ##
[[ -z "$_JAIL" ]] && echo "Empty" || echo "Not empty"</kbd>
OR
#!/bin/bash
###################################
### Warning: Portability issue. ###
###################################
_JAIL="$1"
## note double bracket syntax:
if [[ -z "$_JAIL" ]]
then
echo "Please set \$_JAIL"
else
echo "Setting up jail at $_JAIL"
//call setjail()
// setjail
fi
Example: Determine if a bash variable is empty
Finally here is a script to demonstrate the various possibilities with bash/sh/posix based shell:
#!/bin/bash
JAIL="/nginx"
HINT=""
# Do three possibilities for $JAIL ##
for i in 1 2 3
do
case $i in
1) JAIL="/nginx/jail"; HINT="value set";;
2) JAIL=""; HINT="value set to empty string";;
3) unset JAIL; HINT="\$JAIL unset";;
esac
###############################################
# Update user with actual values and action
# $JAIL set to a non-empty string (1)
# $JAIL set to the empty string (2)
# $JAIL can be unset (3)
################################################
echo "*** Current value of \$JAIL is '$JAIL' ($HINT) ***"
## Determine if a bash variable is empty or not ##
if [ -z "${JAIL}" ]; then
echo "JAIL is unset or set to the empty string"
fi
if [ -z "${JAIL+set}" ]; then
echo "JAIL is unset"
fi
if [ -z "${JAIL-unset}" ]; then
echo "JAIL is set to the empty string"
fi
if [ -n "${JAIL}" ]; then
echo "JAIL is set to a non-empty string"
fi
if [ -n "${JAIL+set}" ]; then
echo "JAIL is set, possibly to the empty string"
fi
if [ -n "${JAIL-unset}" ]; then
echo "JAIL is either unset or set to a non-empty string"
fi
done
## syntax 1 ##
if [[ -z "$variable" ]]; then
echo "Empty $variable"
else
echo "Do whatever you want as \$variable is not empty"
fi
## Syntax 2 ##
[[ -z "$variable" ]] && echo "Empty" || echo "Not empty"
## Syntax 3 ##
[ -z "$var" ] && echo "Empty: Yes" || echo "Empty: No"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment