Korn Shell 93: A better if structure with many tests

Writing korn shell scripts you will often come across if structures that look something like the following. It works well but the if structure doesn’t lend itself for quick reading.

if [[ MYVAR != "potato" ]] && [[ MYVAR != "acorn" ]] && [[ MYVAR != "pizza" ]] && [[ MYVAR != "apple" ]]; then

We can make it far more readable without losing the functionality:

if [[ MYVAR != @(potato|acorn|pizza|apple) ]]; then

The “@(potato|acorn|pizza|apple)” is effectively a short cut to a case structure. So, let’s expand that to the full case statement:

case ${MYVAR} in
        # commands go here
Share Button