Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of the operator || in linux shell?

Tags:

linux

shell

I found this piece of code in a .sh script:

 (test -x "$1" || which "$1") 

What does this operator || mean?

like image 904
aldo85ita Avatar asked Nov 25 '12 16:11

aldo85ita


2 Answers

it means:

if the first command succeed the second will never be executed

like image 69
doniyor Avatar answered Oct 06 '22 03:10

doniyor


It's equivalent to boolean "or" with short-circuiting evaluation, such that it will execute the second command only if the first returns some value corresponding to "false". For example:

false || echo "foo"

echoes "foo", while

true || echo "foo"

Prints nothing. The && operator provides a complimentary operation.

like image 27
Gian Avatar answered Oct 06 '22 02:10

Gian