0

I tried the code in this SO post - How to evaluate a boolean variable in an if block in bash? and it does not work. It looks like there are no booleans in BASH. Is there a flawless workaround which lets me set and check booleans in BASH ?

My code -

#!/bin/bash
flag=true
if [ $flag ]
echo 'True'
#flag=false
#echo 'Now changed to false' 
fi

Even if flag=false in line 2, output is still True. Why ?

Community
  • 1
  • 1
Steam
  • 8,628
  • 27
  • 78
  • 118
  • Please see Jens answer in the link provided. if $myVar; then ... ;fi logic seems to have a security flaw. – Steam Aug 01 '13 at 22:35

2 Answers2

10

Try without square brackets, like this

#!/bin/bash
flag=true
if $flag ; then
   echo 'True'
   #flag=false
   #echo 'Now changed to false' 
fi
Ernest Friedman-Hill
  • 79,064
  • 10
  • 147
  • 183
Cristian Meneses
  • 3,963
  • 19
  • 31
  • 1
    +1 for correct answer. Note also that it's "#!", not "!#", on the first line of a script. – Ernest Friedman-Hill Aug 01 '13 at 22:25
  • What is the difference between brackets and no brackets ? – Steam Aug 01 '13 at 22:28
  • 1
    Without brackets, it attempts to execute the value as a command. It works here because the `true` command returns true, and the `false` command returns false. You can also set `flag="rm -r /*"`, and have the condition delete all your files. – that other guy Aug 01 '13 at 22:34
  • Please see Jens answer in the link provided. if $myVar; then ... ;fi logic seems to have a security flaw. – Steam Aug 01 '13 at 22:35
  • @blasto, whether it's a security issue or not depends on the script and where the value of the variable comes from. It certainly could be a problem sometimes. – Ernest Friedman-Hill Aug 01 '13 at 22:47
4

It evaluates to true because of this part of 'man test' -

 [ expression ]
         string        True if string is not the null string.

You need to use something like -

#!/bin/bash
flag=1
if [ ${flag} -eq 1 ]
then
    echo 'True'
    #flag=0
    #echo 'Now changed to false' 
fi
Omeganon
  • 191
  • 3
  • btw, why did you put flag in {} brackets ? cant we do "$flag" == 1 instead ? – Steam Aug 01 '13 at 22:36
  • `${flag}` and `$flag` are the same. You shouldn't use double quotes and `==` here, though, because you want to do a numeric comparison, not a string comparison. – Ansgar Wiechers Aug 01 '13 at 22:40
  • @AnsgarWiechers - thanks. but, isn't numeric and string comparison the same thing here ? We are just creating a flag and checking its state. – Steam Aug 01 '13 at 22:41
  • @blasto. I use ${} out of habit. It prevents bash from trying to interpret the contents of the variable. – Omeganon Aug 08 '13 at 22:49