DailyWtf

14th March 2015 at 1:09am

Friday 13th March 2014

What better way to test if a variable is set in bash, than to use it in a syntactically incorrect statement in a sub-shell to that you can capture the abortive exit code from the child? Yup, I can't think of a better way either!</sarcasm>

#!/bin/bash
if ( [ -z $notset_var ] ) ; then
    notset_var="set"
fi
if ( [ ! -z $notset_var ] ) ; then
    do_something
fi