case

case

The case command in Linux is a conditional statement that can be used to select from a set of possible values. It is a powerful tool that can be used to make decisions based on the value of a variable.

The case command is used in the following syntax:

case variable in
  pattern1)
    command1
    ;;
  pattern2)
    command2
    ;;
  *)
    default_command
    ;;
esac

The variable is the variable that you want to test.

The pattern1, pattern2, and * are patterns that you want to match against the value of the variable.

The command1, command2, and default_command are the commands that you want to run if the variable matches the pattern.

For example, the following code will print the day of the week if the variable day is set to Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday:

day="Monday"

case $day in
  "Monday")
    echo "The day is Monday."
    ;;
  "Tuesday")
    echo "The day is Tuesday."
    ;;
  "Wednesday")
    echo "The day is Wednesday."
    ;;
  "Thursday")
    echo "The day is Thursday."
    ;;
  "Friday")
    echo "The day is Friday."
    ;;
  "Saturday")
    echo "The day is Saturday."
    ;;
  "Sunday")
    echo "The day is Sunday."
    ;;
  *)
    echo "The day is not set."
    ;;
esac

This code will print the following output:

The day is Monday.

The case command is a powerful and versatile command that can be used to make decisions based on the value of a variable. It is a simple and easy-to-use command that can be used in a variety of contexts.

Here are some additional things to note about the case command:

  • The case command can be used to make decisions based on the value of a variable.

  • The case command can be used to match against patterns.

  • The case command can be used to run multiple commands.

  • The case command is a simple and easy-to-use command.

help

Last updated