sh: boolean value? 

> How can I assign boolean value to a varible and test it later:
>
> if [ $b ] ; then echo b is true! ; fi
> if [ !$b ] ; then echo b is false! ; fi
> the above code will say that b is both true & false no matter what
> value I give to b: 0,1, true & false.

man test — the contents of b are not parsed, it just checks whether it's set to something or not:

$ b=
$ if [ "$b" ] ; then echo empty string = true
> else echo empty string = false; fi
empty string = false
$ b=something
$ if [ "$b" ] ; then echo non-empty string = true
> else echo non-empty string = false; fi
non-empty string = true

/* era */

sh: boolean value? 

Old-timers use `case': it's always been a shell builtin.

b=0
...
case $b in
0)
    echo b is still zero
    ;;
*)
    echo b is no longer zero
    ;;
esac

Tim.

documented on: 1999.09.22 Wed 15:19:48