Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound if statements with multiple expressions in Bash

I would like to recreate something like this

if ( arg1 || arg2 || arg 3) {}

And I did got so far, but I get the following error:

line 11: [.: command not found

if [ $char == $';' -o $char == $'\\' -o $char == $'\'' ]
then ...

I tried different ways, but none seemed to work. Some of the ones I tried.

like image 892
david Avatar asked Jun 29 '12 19:06

david


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does %% mean in bash?

The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.

What is bash if [- N?

A null string in Bash can be declared by equalizing a variable to “”. Then we have an “if” statement followed by the “-n” flag, which returns true if a string is not null. We have used this flag to test our string “name,” which is null.


1 Answers

For Bash, you can use the [[ ]] form rather than [ ], which allows && and || internally:

if [[ foo || bar || baz ]] ; then
  ...
fi

Otherwise, you can use the usual Boolean logic operators externally:

[ foo ] || [ bar ] || [ baz ]

...or use operators specific to the test command (though modern versions of the POSIX specification describe this XSI extension as deprecated -- see the APPLICATION USAGE section):

[ foo -o bar -o baz ]

...which is a differently written form of the following, which is similarly deprecated:

test foo -o bar -o baz
like image 123
Charles Duffy Avatar answered Oct 21 '22 10:10

Charles Duffy