Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare and use Boolean variables in a shell script?

I tried to declare a Boolean variable in a shell script using the following syntax:

variable=$false  variable=$true 

Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using Boolean variables as expressions correct?

if [ $variable ]  if [ !$variable ] 
like image 383
hassaanm Avatar asked Jun 01 '10 21:06

hassaanm


People also ask

How do you set a boolean value in bash?

There are no Booleans in Bash Wherever you see true or false in Bash, it's either a string or a command/builtin which is only used for its exit code. where the command is true . The condition is true whenever the command returns exit code 0.

How do you declare a Boolean variable?

To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.

How do you declare a value to a variable in shell script?

We can declare the variable in shell scripting as below: variable_name=value, when we declare the variable, there is no space between the variable name, assignment operator, and its value.


2 Answers

Revised Answer (Feb 12, 2014)

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

Original Answer

Caveats: https://stackoverflow.com/a/21210966/89391

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

From: Using boolean variables in Bash

The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson's comment about bash builtin true on Jun 2, 2010 only applies to the original answer, not the revised.

like image 156
miku Avatar answered Sep 24 '22 17:09

miku


TL;DR

my_bool=true  if [ "$my_bool" = true ] 

Issues with Miku's (original) answer

I do not recommend the accepted answer1. Its syntax is pretty, but it has some flaws.

Say we have the following condition.

if $var; then   echo 'Muahahaha!' fi 

In the following cases2, this condition will evaluate to true and execute the nested command.

# Variable var not defined beforehand. Case 1 var=''  # Equivalent to var="".      # Case 2 var=                                 # Case 3 unset var                            # Case 4 var='<some valid command>'           # Case 5 

Typically you only want your condition to evaluate to true when your "Boolean" variable, var in this example, is explicitly set to true. All the other cases are dangerously misleading!

The last case (#5) is especially naughty because it will execute the command contained in the variable (which is why the condition evaluates to true for valid commands3, 4).

Here is a harmless example:

var='echo this text will be displayed when the condition is evaluated' if $var; then   echo 'Muahahaha!' fi  # Outputs: # this text will be displayed when the condition is evaluated # Muahahaha! 

Quoting your variables is safer, e.g. if "$var"; then. In the above cases, you should get a warning that the command is not found. But we can still do better (see my recommendations at the bottom).

Also see Mike Holt's explanation of Miku's original answer.

Issues with Hbar's answer

This approach also has unexpected behavior.

var=false if [ $var ]; then   echo "This won't print, var is false!" fi  # Outputs: # This won't print, var is false! 

You would expect the above condition to evaluate to false, thus never executing the nested statement. Surprise!

Quoting the value ("false"), quoting the variable ("$var"), or using test or [[ instead of [, do not make a difference.

What I do recommend:

Here are ways I recommend you check your "Booleans". They work as expected.

my_bool=true  if [ "$my_bool" = true ]; then if [ "$my_bool" = "true" ]; then  if [[ "$my_bool" = true ]]; then if [[ "$my_bool" = "true" ]]; then if [[ "$my_bool" == true ]]; then if [[ "$my_bool" == "true" ]]; then  if test "$my_bool" = true; then if test "$my_bool" = "true"; then 

They're all pretty much equivalent. You'll have to type a few more keystrokes than the approaches in the other answers5, but your code will be more defensive.


Footnotes

  1. Miku's answer has since been edited and no longer contains (known) flaws.
  2. Not an exhaustive list.
  3. A valid command in this context means a command that exists. It doesn't matter if the command is used correctly or incorrectly. E.g. man woman would still be considered a valid command, even if no such man page exists.
  4. For invalid (non-existent) commands, Bash will simply complain that the command wasn't found.
  5. If you care about length, the first recommendation is the shortest.
like image 30
Dennis Avatar answered Sep 24 '22 17:09

Dennis