#!/bin/bash old-way() { local OX=$X SX=${X-y} X=$X X="blah" if [ "$SX" = y ]; then X=$OX else unset X fi } new-way() { local OX=$X SX=${X-n} X="blah" if [ "$SX" = n ]; then unset X else X=$OX fi } baseline() { X="blah" } echo "X unset - using old way" unset X old-way ANSWER=${X+"X is set"} [[ -z ${ANSWER} ]] && echo "X is not set" || echo "X is set to $X" echo echo echo "X set - using old way" X="A" old-way ANSWER=${X+"X is set"} [[ -z ${ANSWER} ]] && echo "X is not set" || echo "X is set to $X" echo echo echo "X unset - using new way" unset X new-way ANSWER=${X+"X is set"} [[ -z ${ANSWER} ]] && echo "X is not set" || echo "X is set to $X" echo echo echo "X set - using new way" X="A" new-way ANSWER=${X+"X is set"} [[ -z ${ANSWER} ]] && echo "X is not set" || echo "X is set to $X" echo echo echo "X unset - baseline" unset X baseline ANSWER=${X+"X is set"} [[ -z ${ANSWER} ]] && echo "X is not set" || echo "X is set to $X" echo echo echo "X set - baseline" X="A" baseline ANSWER=${X+"X is set"} [[ -z ${ANSWER} ]] && echo "X is not set" || echo "X is set to $X"