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"

- [Instructor] The case statement is a control structure which lets us define code to run based on particular conditions. We provide it a series of values and whichever one matches the given input is what Bash runs. A case statement starts out with the word case followed by a variable that you're testing and the word in. I'll start my script by defining a variable. I'll write animal equals dog. Then I'll write case, provide that variable to test and provide the keyword in. Then on the next line, I'll put a condition to test. (keyboard clicks) This will test if the value of the variable animal is equal to bird. That test condition comes before a right parenthesis to indicate the end of the test. That's followed up by whatever I want to do when the value matches. And then two semicolons to tell bash that you're done with this condition, and you can keep adding test conditions. We can use the pipe character to…

Contents