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 ]
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.
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”.
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.
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.
my_bool=true if [ "$my_bool" = true ]
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.
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.
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.
man woman
would still be considered a valid command, even if no such man page exists.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With