Village
Village

Reputation: 24373

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?

Upvotes: 294

Views: 250434

Answers (5)

Champignac
Champignac

Reputation: 725

It looks like that there is no other way than writing your own nop as a function. : does not the intended job, as it affects the exit status; maybe you'll find a builtin in the manual that does not change the exit status, but I've failed to exhibit such a one. You may say:

nop() { return; } # may return $?
bad() { return 42; }

bad; nop; echo $? # "nop" does nothing and keeps status; echo echoes 42
bad; :  ; echo $? # ":" does nothing but changes status; echo echoes 0

Upvotes: 1

user1855805
user1855805

Reputation: 161

instead of :, true, false I use

echo -n ""

It avoid empty line in terminal

You could also do it more concisely as:

echo -n

Upvotes: 0

Barmar
Barmar

Reputation: 780688

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.

Upvotes: 547

KeyNone
KeyNone

Reputation: 9150

Although I'm not answering the original question concering the no-op command, many (if not most) problems when one may think "in this branch I have to do nothing" can be bypassed by simply restructuring the logic so that this branch won't occur.

I try to give a general rule by using the OPs example

do nothing when $a is greater than "10", print "1" if $a is less than "5", otherwise, print "2"

we have to avoid a branch where $a gets more than 10, so $a < 10 as a general condition can be applied to every other, following condition.

In general terms, when you say do nothing when X, then rephrase it as avoid a branch where X. Usually you can make the avoidance happen by simply negating X and applying it to all other conditions.

So the OPs example with the rule applied may be restructured as:

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

Just a variation of the above, enclosing everything in the $a < 10 condition:

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

(For this specific example @Flimzys restructuring is certainly better, but I wanted to give a general rule for all the people searching how to do nothing.)

Upvotes: 12

Jonathan Hall
Jonathan Hall

Reputation: 79536

You can probably just use the true command:

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

An alternative, in your example case (but not necessarily everywhere) is to re-order your if/else:

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

Upvotes: 64

Related Questions