Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash if with or and negation

Tags:

bash

why does:

#!/bin/bash
wtf=false
if [ $wtf ] || [ ! -f filethatexists.whatever ]
then
 echo "WTF1"
fi
if [ ! -f filethatexists.whatever ]
then
 echo "WTF2"
fi

print:

WTF1

instead of nothing? It is especially perplexing that the second form works as expected and the first not.

like image 233
i30817 Avatar asked Sep 26 '10 22:09

i30817


People also ask

How do you negate if statements in bash?

How to negate an if condition in a Bash if statement? (if not command or if not equal) To negate any condition, use the ! operator, for example: if ! <test-command>; then <command-on-failure>; fi .

How do you negate an IF condition?

Negation of "If A, then B". To negate a statement of the form "If A, then B" we should replace it with the statement "A and Not B". This might seem confusing at first, so let's take a look at a simple example to help understand why this is the right thing to do.

How do you negate a condition in shell script?

NegationWhen we use the not operator outside the [[, then it will execute the expression(s) inside [[ and negate the result. If the value of num equals 0, the expression returns true. But it's negated since we have used the not operator outside the double square brackets.

What does != Mean in bash?

The origin of != is the C family of programming languages, in which the exclamation point generally means "not". In bash, a ! at the start of a command will invert the exit status of the command, turning nonzero values to zero and zeroes to one.


1 Answers

The basic test

[ $wtf ]

tests whether the string in the middle is empty or not.

Since $wtf contains the string 'false', the test returns true, or exit status 0 for success, because 'false' is not the same as the empty string '' — and hence you get WTF1 as the response.

Try with:

wtf=''

As pointed out by Gordon Davisson (and Dennis Williamson), it is a good idea to be careful with strings that you are testing. Indeed, I should have stated that I would always use [ -n "$wtf" ] or [ -z "$wtf" ] to test whether a variable is set, because that was necessary when I was learning shell, once upon a quarter century ago. I've had counter stories from Bash afficionados that you don't have to worry about it in Bash - however, I think the code here provides a counter-example that in fact you do still have to worry about it.

So, some best practices:

  • Enclose tested variables in double quotes, or
  • (In Bash), use [[ $wtf ]] which does know how to handle the variable expansion.
  • Use the -n or -z tests to test for non-empty or empty values.

There can be exceptions to the rules - but you will not go far wrong following them.

Consider the code:

wtf="1 -eq 0"
 [  $wtf  ]  && echo "WTF0"
[[  $wtf  ]] && echo "WTF1"
wtf="false"
 [  $wtf  ]  && echo "WTF2"
[[  $wtf  ]] && echo "WTF3"
wtf=""
 [  $wtf  ]  && echo "WTF4"
[[  $wtf  ]] && echo "WTF5"
wtf="false"
 [ "$wtf" ]  && echo "WTF6"
[[ "$wtf" ]] && echo "WTF7"
wtf=""
 [ "$wtf" ]  && echo "WTF8"
[[ "$wtf" ]] && echo "WTF9"

That produces:

WTF1
WTF2
WTF3
WTF6
WTF7

with both bash and ksh (as found on MacOS X 10.6.4, when run with 'bash testcode.sh' or 'ksh testcode.sh'). A real Bourne shell (if you can still find such a thing) would object to the double-bracket operations - it would not be able to find the command '[[' on $PATH.

You can extend the testing to cover more cases ad nauseam.

like image 160
Jonathan Leffler Avatar answered Sep 22 '22 04:09

Jonathan Leffler