Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a condition is false

It is seems to be an easy question, I wonder why googling didn't give anything helpful -- nor in StackOverflow, nor in tutorials. I just need to check using bash that a condition is false.

Of what I found I tried

if ! [ 0==2 ]; then echo Hello; fi 

and

if [ ! 0==2 ]; then echo Hello; fi  

none of them print Hello.

I found only two similar questions, but the end answer in both cases was restructured code to not use the "false" condition.

like image 800
Hi-Angel Avatar asked Jul 11 '14 09:07

Hi-Angel


People also ask

Does bash have Boolean?

Bash does have Boolean expressions in terms of comparison and conditions. That said, what you can declare and compare in Bash are strings and numbers. That's it. 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.


1 Answers

Do you mean:

if ! [ 0 == 2 ]; then   echo Hello; fi 

You lacked space around the equality operator.

This might be the time to read http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html - especially the sections about if then else and operators. I usually have this open when I am writing scripts..

like image 148
wojciii Avatar answered Oct 04 '22 01:10

wojciii