This is a hack I just discovered:
DEBUG=true
$DEBUG && echo "Hello"
And this just happens to work because true
is an actual command which returns 0, and the &&
operator is happy with that:
» true
» echo $?
0
Is there a non-hackish way of execute a piece of code if a variable is set, to whatever value, except the empty string? Something like this, but as a readable one-liner (like the one above):
myvar="ggg"
if [ "$myvar" != "" ] ; then echo "Hello" ; fi