271

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Village
  • 22,513
  • 46
  • 122
  • 163

4 Answers4

511

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 5
    @SaintHax No, it doesn't run `true`. It's built into the shell, and does nothing. – Barmar May 26 '17 at 19:29
  • 1
    it is shell built in (you are correct), it returns true (status 0) just like true does, and it is more difficult to read. Bash is a DevOps language, and if `:` is written by someone on my team, I'd have them change it. – SaintHax May 29 '17 at 19:51
  • 4
    @SaintHax If someone used `true` in a non-conditional context, I'd have them change it. `if true` is fine, `then true` seems wrong. – Barmar May 29 '17 at 21:29
  • 3
    Of course, you can simply rewrite the code to use `if ! condition; then do something; fi` instead of `if condition; then :; else do something; fi`. – Barmar May 29 '17 at 21:30
  • I agree with you. This example could be written if/elif/else using compound conditions and would be better. – SaintHax May 30 '17 at 13:50
  • 1
    @Barmar maybe it's usefull in this case while (condition) do : done – kingsjester Nov 18 '22 at 10:41
  • @kingsjester The code is not on a loop, hence `while ` is not useful – alper Nov 18 '22 at 12:57
  • @alper of course not in this specific case (here you can avoid : by using the code proposed by barmar), but more generally, if you need to wait until a condition is false, you can use the while (condition) do : done – kingsjester Nov 18 '22 at 13:33
  • 2
    E.g. `while ! ping -c 1 hostname; do : done` to wait for ping to succeed. @kingsjester – Barmar Nov 18 '22 at 15:27
60

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
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 8
    If you don't test the result both `true` and `false` are effectively no-ops as far as the script is concerned, but in principle they *could* fork in some shells that accept this syntax so perhaps `:` is better. – dmckee --- ex-moderator kitten Jul 11 '13 at 01:31
  • 3
    The question is tagged bash. In bash, `true` and `false` are built-ins. For older shells, `true`, `false`, and even `:` might be external commands (though you'd probably see the latter only in *very* old shells). – Keith Thompson Jan 12 '14 at 04:37
  • 8
    I prefer `:` simply because it is more explicit what the intent is – Freedom_Ben Sep 19 '14 at 21:12
  • 1
    The result `false` may not be a no-op, because if `set -e` is used (exit on non-zero) then `false` will cause the script to exit immediately. `true` is a better no-op. `:` is a real no-op, but I have to agree that it is unreadable and I've been programming bash for 30 years and I didn't know about it... – Mark Lakata Dec 08 '21 at 18:48
12

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.)

Community
  • 1
  • 1
KeyNone
  • 8,745
  • 4
  • 34
  • 51
  • 7
    One reason someone might want to have a no-op is when initially structuring the code with the idea that one will add the "add something here later" without having inadvertent side effects. E.g. if one is putting an if-then-else-fi in a .bashrc, if you put `echo "put stuff here"` it'll break some of the non-login uses (e.g. scp gets broken), and only whitespace/comments causes a runtime error. – Aphoid Jan 02 '18 at 16:38
  • 1
    Also, personally, I prefer to write code that is more "human friendly". It's often possible to restructure code to be more concise or even efficient. But when that's not critical, I prefer my code to be more readable for whichever developer comes along afterwards. (I also like commenting my code.) – osullic Jan 06 '21 at 10:08
0

instead of :, true, false I use

echo -n ""

It avoid empty line in terminal

You could also do it more concisely as:

echo -n
user1855805
  • 137
  • 1
  • 1
  • 9