From the course: Linux Bash Shells and Scripts: Streamlining Tasks and Enhancing Workflows with Automation

Unlock this course with a free trial

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

Case statements

Case statements

- [Instructor] Case statements can be handy. The syntax is a bit of a challenge, so you need to look at this kind of carefully. Otherwise, it could be frustrating. Case statement uses the word case, then you have some sort of expression, typically the value of a variable, then the word in. And then on a new line, you have some sort of pattern, and the patterns can get kind of sophisticated. And then you have a closed paren. There's no open paren there's just a closed paren. Then you have a list of commands that will get executed if the expression matches the pattern. And then you end that list with two semicolons. So you can have a bunch of lines. And then at the end, after all the lines, put two semicolons. And then you can have another pattern and so on. And then you end the case statement with the word case spelled backwards, esac. Let's look at an example. So we have case, and then we're going to get the value of the variable ans, the answer. And then we have the word in. And then…

Contents