views:

581

answers:

1

Title.

The way I tried declaring a boolean variable is by:

variable=$false

variable=$true

Is the syntax correct? Also, if I wanted to update that variable would I just do the same format? Finally, is the following the proper syntax for using boolean variables as expressions:

if [ $variable ]

if [ !$variable ]

Thanks! Any help is appreciated!

+1  A: 

From: Using boolean variables in Bash

the_world_is_flat=true
# ...do something interesting...
if $the_world_is_flat ; then
    echo 'Be careful not to fall off!'
fi
The MYYN
Thanks! How would I check for NOT $variable?"if !$variable" does not work.
hassaanm
Oh, never mind; I got it. It's:if ! $variable; then...fi
hassaanm
To explain what is happening: the `if` statement is executing the contents of the variable which is the Bash builtin `true`. Any command could be set as the value of the variable and its exit value would be evaluated.
Dennis Williamson