Search notes:

Bash: conditional constructs

True and false

if (exit 0); then
   echo exit 0 is true
else
   echo exit 0 is false
fi
# exit 0 is true

if (exit 1); then
   echo exit 1 is true
else
   echo exit 1 is false
fi
# exit 1 is false

if [ ] ; then
   echo empty is true
else
   echo empty is false
fi
# empty is false

if [ " " ]; then
   echo '" "' is true
else
   echo '" "' is false
fi
# " " is true

if [ 0 ]; then
   echo 0 is true
else
   echo 0 is false
fi
# 0 is true

if [ 1 ]; then
   echo 1 is true
else
   echo 1 is fale
fi
# 1 is true

if [ foo ]; then
   echo foo is true
else
   echo foo is false
fi
# foo is true
Github repository about-Bash, path: /conditional-constructs/true-and-false

String and numerical comparison

Strings need to be compared with <, >, <= etc., numbers with -gt, -lt, -eq etc.
#!/bin/bash

if [[ 9  >  111 ]]; then echo "9   > 111"; else echo "9  <= 111"; fi  # 9   > 111
if [[ 9 -gt 111 ]]; then echo "9 -gt 111"; else echo "9 -lt 111"; fi  # 9 -lt 111

if [[ X  >  ABC ]]; then echo "X   > ABC"; else echo "X  <= ABC"; fi  # X   > ABC
if [[ AB >    X ]]; then echo "AB >    X"; else echo "AB <=   X"; fi  # AB <=   X
Github repository about-Bash, path: /conditional-constructs/string-and-numeric-comparison

[[ vs [

#!/bin/bash

A_VAR='foo'
CONTAINS_SPACES='foo bar baz'

# With [[, unset/empty variables need not be
# quoted:
if [[ $A_VAR == $NOT_A_VAR ]]; then
  echo "A_VAR == NOT_A_VAR"
else
  echo "A_VAR != NOT_A_VAR"
fi

# But the need to be quoted with "…" in [ constructs ]
if [ $A_VAR = "$NOT_A_VAR" ]; then # Note: == is not POSIX, it's a bashism
  echo "A_VAR == NOT_A_VAR"
else
  echo "A_VAR != NOT_A_VAR"
fi

# With [[, variables that contain spaces need not
# be quoted:
if [[ $CONTAINS_SPACES == 'foo bar baz' ]]; then
  echo "CONTAINS_SPACES == foo bar baz"
else
  echo "CONTAINS_SPACES != foo bar baz"
fi

# But with [, the need be quoted.
if [ "$CONTAINS_SPACES" = 'foo bar baz' ]; then # Note: == is not POSIX, it's a bashism
  echo "CONTAINS_SPACES = foo bar baz"
else
  echo "CONTAINS_SPACES != foo bar baz"
fi
Github repository about-Bash, path: /conditional-constructs/[-vs-[[

>

In [ foo > bar], the > is intepreted as a redirection operator. Thus, [ foo > bar ] creates/overwrites the file bar.
in [[ foo >> bar ], the > compares its left and right side, as probably intended.

Exit status

#!/usr/bin/bash

# if takes a command and acts depending on the command's
# exit value.
# The [ ] is not part of if, it IS a command
# 

if ./exit_0.sh; then
  echo "Yes"
else
  echo "No"
fi

if ./exit_1.sh; then
  echo "Yes"
else
  echo "No"
fi
Github repository about-Bash, path: /conditional-constructs/if_command

exit_0.sh

#!/usr/bin/bash

#
#  Used in ./if_command
#

echo "exit 0"
exit 0

exit_1.sh

#!/usr/bin/bash

#
#  Used in ./if_command
#

echo "exit 1"
exit 1

if fi

#!/bin/bash

if [ $# -lt 2 ]; then
  echo "Usage: if_fi   num-1  num-2"
  exit 1
fi

if [ $1 -gt $2 ]; then
  echo "$1 > $2"
fi

if (( $1-$2 )); then
  echo " $1-$2 = $(($1-$2))"
fi

if ! (( $1-$2 )); then
  echo " $1-$2 = 0 "
fi
Github repository about-Bash, path: /conditional-constructs/if_fi

case … esac

case_() {

    case $1 in
      foo*|bar*   ) echo $1 matches 'foo*' or 'bar*' ;;
      baz*        ) echo $1 matches 'baz*';;
      *           ) echo $1 matches neither foo, bar or baz;;
    esac

}

case_ foo
case_ bar
case_ baz
case_ 42


#
# vim: ft=sh
#
Github repository about-Bash, path: /conditional-constructs/case

Testing for equalness with ==

== tests case sensitively for equalness
# vim: ft=sh

is_equal() {

  val_1=$1
  val_2=$2

  if [ "$val_1" == "$val_2" ]; then
    echo "$val_1 is equal $val_2"
  else
    echo "$val_1 is not equal $val_2"
  fi

}

is_equal  FooBarBaz  Bar             # FooBarBaz is not equal Bar
is_equal  FooBarBaz  FooBarBaz       # FooBarBaz is equal FooBarBaz
is_equal  FooBarBaz  foobarbaz       # FooBarBaz is not equal foobarbaz
is_equal "foo bar z"  "foo bar z"    # foo bar z is equal foo bar z
Github repository about-Bash, path: /conditional-constructs/equal-equal

See also

bash

Index