Using boolean variables in Bash

Today I stumbled upon a weblog post by Mark Dominus on a “novel” way of using flag variables in shell code, mainly because I have been using that technique since my early days of shell programming. Let me introduce the syntax:

the_world_is_flat=true
# ...do something interesting...
if $the_world_is_flat ; then
  echo 'Be careful not to fall off!'
fi

This example may look familiar to some of my readers, because in fact it is taken from the Bill tutorial. Some modules included in Bill use this kind of syntax extensively. Some care must be taken, because we are directly executing the contents of a variable in the if clause, so you would not better use this with values entered by the user, but I find very convenient to directly expand and evaluate true and false from variables when they come from “trusted” code.

I learned this trick several years ago from my friend Andrés when we were working together. I do not know when did he learn this syntax, but I am sure that the thing is not as new as it sounds.

Oops, almost forgot: have a nice 2009!

Tags:

6 comments

  1. Mmm, I’m not sure about if I really read this somewhere, maybe I just probe it one day and… magic! :)

    I usually do if ${var:=true} ; then …, so if var is not defined previously the condition is always true (or false) and no get a syntax error near expected token ‘;’.

    I really hate from bash novels the abusive use of command listings, you known: a && b || c && ! a || b && d… if exists! One day I write a post about things that I hate… one day when I’ve a lot of time ;)

  2. You are right, there is an interesting amount of features which can be used in Bash to improve readability of code and are useful per se. It is a pity that most shell code is still written like it was 1978… ;-)

  3. I was trying to use a similar technique at work today, but it wasn’t working. Problem was that I using [] around my $flag. I’m no bash expert, so I was a bit confounded. Now I get it (I think): inside [], $flag is evaluated as a string. I ended up using a hack, but now that I know what’s going on, I can use this more elegant idiom.

    Since your post is near the top for “bash boolean”, I thought it might be useful to include a note on this difference, since to the uninitiated, it might be hard to spot. When I first read your post, I thought, “Hey, that’s exactly what I was doing. Why didn’t it work?”

  4. Wyatt, if you use those square brackets around your test, you are in fact using an visually alternative way to just call the test command. The test command can have parameters and qualifiers, like this:

    $ test $var1 -gt $var2

    which equals this:

    [ $var1 -gt $var2 ]

    In bash, an If-structure always expects a boolean value (or a 1 or 0, don’t know exactly), and that’s the value that the test command will return to the if-structure. If the content of a variable is already 0 or 1, then the content of that variable may be used as a condition in the if-structure. This is why you don’t need square brackets around a boolean variable, while you do need them for other testing, since you will be doing the tests with the test command instead of returning the content of the variable directly to the if-structure.

    I hope I’ve been helpful and correct!

  5. Thanks a lot for the post. I using [] around my $valid_status . The post helped me to fix it. :-)

Leave a comment


Bad Behavior has blocked 262 access attempts in the last 7 days.