bash input parameter logic The following works fine and I'm not having to repeat any commands once logic is evaluated. However, it feels as though I'm doing a lot of seperate checks. I'm wondering if I could do this more efficiently?

#!/bin/bash

#The script can have 0, 1 or 2 input parameters
#parameter $1 can be one of 4 values ('foo','bar','chu','test')
#parameter $2 can only be 'test' and only if $1 is present and not 'test'


if ( [ $# == 1 ] &amp;&amp; [ $1 == 'test' ] ) || ( [ $# == 2 ] &amp;&amp; [ $1 != 'test' ] &amp;&amp; [ $2 == 'test' ] ); then
    echo 'enable testing'
fi

if [ $# == 0 ] || ( [ $# == 1 ] &amp;&amp; [ $1 == 'test' ] ); then
    echo 'do everything'
    exit 0
fi

if ( [ $# == 1 ] &amp;&amp; [ $1 != 'test' ] ) || ( [ $# == 2 ] &amp;&amp; [ $2 == 'test' ] ); then
    case "$1" in
        'foo')
            echo 'only do foo'
            exit 0
            ;;
        'bar')
            echo 'only do bar'
            exit 0
            ;;
        'chu')
            echo 'only do chu'
            exit 0
            ;;
        *)
            echo 'not valid'
            exit 1
    esac
else
    echo 'not valid'
    exit 1
fi

exit 0
You can achieve the same but simpler if you reorder the validation steps.

If there are two arguments and both are test, that's invalid:

[ "$1" = test ] &amp;&amp; [ "$2" = test ] &amp;&amp; invalid


Where invalid is a helper function:

invalid() {
    echo not valid
    exit 1
}


If the second argument is test, then enable testing,
or else if the first argument is test then enable testing and shift the arguments:

if [ "$2" = test ]; then
    echo enable testing
elif [ "$1" = test ]; then
    echo enable testing
    shift
fi


At this point, the should be 0 or 1 arguments, not more.
That was a missing validation step in the original code:

[ $# -le 1 ] || invalid


If we reach this point,
there must be 0 or 1 arguments.
The original case statement can come here as before, without any further if statements. It can use the invalid helper function in the invalid case.