Search⌘ K
AI Features

if and case Statements

Explore the use of if and case statements in Bash scripts to manage conditional operations. Understand how the if statement evaluates boolean expressions, while the case statement matches patterns against a string. Learn when to use each for effective scripting and how to avoid code duplication using specific delimiters in case statements.

Comparing if and case statements

Let’s compare the if and case statements in the if scriptifsmt and case script.casesmt First, we write them in a general form. Here is the result for the if statement:

if CONDITION_1
then
  ACTION_1
elif CONDITION_2
then
  ACTION_2
elif CONDITION_3
then
  ACTION_3
else
  ACTION_4
fi

The case statement looks this way:

case STRING in
  PATTERN_1)
    ACTION_1
     ;;

  PATTERN_2)
    ACTION_2
    ;;

  PATTERN_3)
    ACTION_3
    ;;

  PATTERN_4)
    ACTION_4
    ;;
esac

The differences between the ...