Bash operators

logical operators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
&&
 
    and (logical)
 
    if [ $condition1 ] && [ $condition2 ]
    # Same as:  if [ $condition1 -a $condition2 ]
    # Returns true if both condition1 and condition2 hold true...
 
    if [[ $condition1 && $condition2 ]]    # Also works.
 
||
 
    or (logical)
 
    if [ $condition1 ] || [ $condition2 ]
    # Same as:  if [ $condition1 -o $condition2 ]
    # Returns true if either condition1 or condition2 holds true...
 
    if [[ $condition1 || $condition2 ]]    # Also works.
    # Note that || operator not permitted within [ ... ] construct.

Example:

1
2
X=1
[ $X -ne 0 ] && echo "X isn't zero" || echo "X is zero"

reference

Homepage
Comments

Hide Comments