# cannot properly employ "or" operator in an if statement (bash)

**URL:** <https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377>\
**Category:** Shell Programming and Scripting\
**Created:** [August 12, 2010, 5:44pm UTC](https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377 "2010-08-12T17:44:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodbenito](https://community.unix.com/letter_avatar/goodbenito/32/6_22734026db12803ebb3e059622ac3534.png) [@goodbenito](https://community.unix.com/u/goodbenito)\
**Post date:** [August 12, 2010, 5:44pm UTC](https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377/1 "2010-08-12T17:44:53Z")

</div>

Hi,

I have a variable, $sername, and I would like to display this variable only if it _does not_ contain either of these two tags: \*DTI\*FA\* or \*DIFF\*FA\*.

I think the syntax for my 'or' operator is off. The variable $sername is continuously changing in an outer loop (not shown), but at the moment, as an example, let's say:

```nohighlight
sername=field_mapping

if [[$sername == *DIFF*FA* || *DTI*FA*]]; then echo "Found an FA tag in sername"; else echo "The variable sername is FA-free"; fi

```

But this statement keeps outputting, "Found an FA tag in sername", which isn't right. Can anyone help me with the syntax? It would be nice if it could be done in a case-insensitive manner, too.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [August 12, 2010, 5:52pm UTC](https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377/2 "2010-08-12T17:52:43Z")

</div>

What shell are you using? Checking if a string contains a string isn't a single-operator task I think.

[edit] ah, bash.

```nohighlight
function contains # haystack needle
{
        local H="$1"
        local N="$2"
        local C="${#H}"
        H="${H/$N/}"

        ["${#H}" -lt "${C}"] && return 0
        return 1
}

contains ASDF G || contains ASDF H || echo "Doesn't contain these things"

contains ASDF A || contains ASDF B || echo "Doesn't contain these things"

```

---

<div class="post-metadata">

**Author:** ![sheelscripter](https://community.unix.com/letter_avatar/sheelscripter/32/6_22734026db12803ebb3e059622ac3534.png) [@sheelscripter](https://community.unix.com/u/sheelscripter)\
**Post date:** [August 12, 2010, 8:47pm UTC](https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377/3 "2010-08-12T20:47:50Z")

</div>

to use 'or' in if in bash or bourne , do this:

if [blah] || [blah] || [blah]; then  
blah blah blah

fi

---

<div class="post-metadata">

**Author:** ![goodbenito](https://community.unix.com/letter_avatar/goodbenito/32/6_22734026db12803ebb3e059622ac3534.png) [@goodbenito](https://community.unix.com/u/goodbenito)\
**Post date:** [August 12, 2010, 10:25pm UTC](https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377/4 "2010-08-12T22:25:01Z")

</div>

If I try sheelscripter's suggestion:

```nohighlight

$sername=field_mapping

if [$sername == *map*] || [$sername == *DTI*FA*]; then echo "Found an FA tag in sername"; else echo "The variable sername is FA-free"; fi

```

it is not specific enough, i.e., even though there's the tag "map" in the variable $sername, and I am now calling that in the 'if' statement, it still says that that variable does not contain the tag. In other words, the echo command delivers: "The variable sername is FA-free". So it didn't work.

Any other suggestions? This is bash, by the way. Thanks!

---

<div class="post-metadata">

**Author:** ![agama](https://community.unix.com/user_avatar/community.unix.com/agama/32/21613_2.png) [@agama](https://community.unix.com/u/agama)\
**Post date:** [August 12, 2010, 10:50pm UTC](https://community.unix.com/t/cannot-properly-employ-or-operator-in-an-if-statement-bash/271377/5 "2010-08-12T22:50:27Z")

</div>

Your original if statement was nearly correct. There must be a complete expression on either side of the or:

```nohighlight
if [[$sername == *DIFF*FA* || $sername == *DTI*FA*]]

```

Your second attempt isn't working because

```nohighlight
[expression]

```

is very much different than

```nohighlight
[[expression]]

```

The double bracket expression is interpreted by the shell and the right hand side is treated as a pattern (if not quoted). Single brackets are treated just like any any other command (remember that [ is a link to the test command). If wildcards (\* and ?) are used inside of single brackets, they are expanded as file names in the current directory and NOT treated as patterns. The filename expansion is then passed to the test command.

In my opinion it is best to avoid using single brackets in if and while statements in Kshell and bash. Forking to the test command is very inefficient not to mention the enhancements to the expressions that Kshell and bash provide.
