Bash Logical Operators

In the POSIX shell grammar, the operator && is referred to as AND_IF and || to OR_IF.

Here are a couple of rules about logical operators:

  • If you write test && command, the command will only be executed if the test succeeds.
  • If you write test || command, the command will only be executed if the test fails.

Examples:

1
2
3
4
$ true || echo "Oops" #Nothing
$ false || echo "Oops" #Print "Oops"
$ false && echo "Oops" #Nothing
$ true && echo "Oops" #Print "Oops"

Note: Don’t confuse the logical operator || with the pipe operator |. Check out this post for bash piping.