Search code examples
bashconditional-statementsno-op

What command means "do nothing" in a conditional in Bash?


Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when $a is greater than "10", print "1" if $a is less than "5", otherwise, print "2":

if [ "$a" -ge 10 ]
then
elif [ "$a" -le 5 ]
then
    echo "1"
else
    echo "2"
fi

This makes an error though. Is there a command which will do nothing and also not slow down my script?


Solution

  • The no-op command in shell is : (colon).

    if [ "$a" -ge 10 ]
    then
        :
    elif [ "$a" -le 5 ]
    then
        echo "1"
    else
        echo "2"
    fi
    

    From the bash manual:

    : (a colon)
    Do nothing beyond expanding arguments and performing redirections. The return status is zero.