Linux Tutorial Series

Linux Tutorial Series – 190 – case statement

Here is the video version, if you prefer it:

Today let’s talk about the case statement. case statement is used to replace a lot of elif statements. Here is our entire script with the same functionality it had before, rewritten using a case statement:


case $1 in


echo 'Hello back to you!'



echo 'Hi!'



echo 'You are rude.'



Here is how the case statement works:

  1. You tell case which argument you are testing (more specifically, pattern matching) – we are testing the argument passed to the script in the first position ($1)
  2. case goes through the list of patterns (each pattern ends in a )) and if it finds a matching pattern, the code between the ) and the ;; is executed and then it skips to esac
  3. esac denotes the end of the case statement

The case statement does not evaluate any exit codes, it just matches patterns.

Here are some things to note: (Ward, 2014)⁠

  • multiple strings can be matched with | – if I put ‘Hi!’|’Hi’ I would match both “Hi!” and “Hi” and that line would look like 'Hi!'| ‘Hi’)
  • * matches a case which is unmatched by any other case

Hope you found this useful!


Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 261-262

NewsletterUpdates on interesting things I am doing

Subscribe to my newsletter to keep abreast of the interesting things I'm doing. I will send you the newsletter only when there is something interesting. This means 0% spam, 100% interesting content.

Leave a Reply

Your email address will not be published. Required fields are marked *