Skip to content

Instantly share code, notes, and snippets.

@mblair
Created December 30, 2011 01:19
Show Gist options
  • Save mblair/1537072 to your computer and use it in GitHub Desktop.
Save mblair/1537072 to your computer and use it in GitHub Desktop.
arggggggggghhhhh
#!/usr/bin/env bash
#You need Bash >= 4.2.0 for this.
bash --version | head -n1
set -o errexit
set -o xtrace
unset my_var
if test -v "$my_var"; then
echo "my_var is set"
else
echo "my_var is not set"
fi
my_var=
if test -v "$my_var"; then
echo "my_var is set"
else
echo "my_var is not set"
fi
my_var="a string"
if test -v "$my_var"; then
echo "my_var is set"
else
echo "my_var is not set"
fi
unset my_var
echo ${my_var-default}
my_var="a string"
echo ${my_var-default}
if test -v "$my_var"; then
echo "my_var is set"
else
echo "my_var is not set"
fi
#Do not use parameter expansion!
if test -v my_var; then
echo "my_var is set"
else
echo "my_var is not set"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment