user788171
user788171

Reputation: 17553

bash, how to use OR || operator in If statement?

Can anybody tell me what is wrong with this syntax:

if [ "$1" == "postfix" || "$1" == "all" ]; then
    echo test
fi

Upvotes: 0

Views: 80

Answers (2)

Alfe
Alfe

Reputation: 59426

Use either

if [ "$1" = "postfix" ] || [ "$1" = "all" ]; then

or

if [ "$1" = "postfix" -o "$1" = "all" ]; then

|| combines two commands (and the [ is a command). Within the [ ... ] the operator -o is used for or.

Another way to use || is to use it inside [[ instead of [.

In any way, the correct operator for equality is = inside [ ... ] as it is the POSIX standard (according to man bash). == is supported, though.

Upvotes: 2

Gilles Quénot
Gilles Quénot

Reputation: 185025

If you want to do this, use [[ test form :

if [[ $1 == "postfix" || $1 == "all" ]; then

[[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals Unless you're writing for POSIX sh, we recommend [[

or

if [ "$1" == "postfix" ] || [ "$1" == "all" ]; then

Upvotes: 1

Related Questions