From the course: Learning Bash Scripting

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Selecting behavior using "case"

Selecting behavior using "case" - Bash Tutorial

From the course: Learning Bash Scripting

Selecting behavior using "case"

The case statement is a control structure which lets us define code to run based on particular conditions. We provided a series of values, and whichever matches the given input is what Bash runs. Let's take a look at that. I'll open my script, and I'll clear out what's here. Let's define a variable. I'll call this animal, and I'll set it equal to dog. A case statement starts out with the word case, followed by the variable you're testing and the word in. And then on the next line, we'll put a condition to test. First, we'll see if the value of animal is equal to bird. I'll put that before our right parenthesis to indicate the end of that test. That's followed up by whatever I want to do when the value matches. In this case, I'll echo "Avian", and then I'll put two semicolons to tell Bash that we're done with this condition, and we can just keep adding test conditions. We can use the pipe character to separate items in the list of things to match. For example, this will match dog or…

Contents