๐Ÿ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 50 views

๐Ÿš Bash Case Statements โ€“ Use case, in, and esac for Multi-Condition Scripts


๐Ÿงฒ Introduction to Bash Case Statements โ€“ Simplify Multi-Condition Checks

When a script needs to evaluate multiple possible values of a variable and perform actions accordingly, case statements offer a cleaner alternative to multiple if-elif-else blocks. The Bash case command works like a switch-case construct in other programming languages and is ideal for handling menu selections, argument parsing, and input validation.


๐ŸŽฏ In this article, youโ€™ll learn:

  • How to write a case statement in Bash
  • Syntax and structure of case, in, and esac
  • Pattern matching in case blocks
  • Practical use cases and script examples

๐Ÿงพ Basic Syntax of case Statement

case $variable in
  pattern1)
    command1
    ;;
  pattern2)
    command2
    ;;
  *)
    default_command
    ;;
esac
KeywordPurpose
caseBegins the statement
inBegins the list of patterns
)Ends the pattern and starts code
;;Ends the current block
*Wildcard for unmatched cases
esacEnds the statement (case in reverse)

๐Ÿงช Example 1: Simple Menu Using case

echo "Choose an option: start | stop | status"
read action

case $action in
  start)
    echo "Starting service..."
    ;;
  stop)
    echo "Stopping service..."
    ;;
  status)
    echo "Service status: Running"
    ;;
  *)
    echo "Invalid option"
    ;;
esac

โœ… Sample Run:

Choose an option: start | stop | status  
> start  
Starting service...

๐Ÿงช Example 2: Pattern Matching with Wildcards

read -p "Enter filename: " file

case $file in
  *.txt)
    echo "Text file"
    ;;
  *.sh)
    echo "Shell script"
    ;;
  *)
    echo "Unknown file type"
    ;;
esac

โœ… Output:

Enter filename: test.sh  
Shell script

๐Ÿ’ก You can match file extensions, numbers, or wildcard patterns.


๐Ÿงช Example 3: Case with Command-Line Arguments

case "$1" in
  start)
    echo "App started"
    ;;
  stop)
    echo "App stopped"
    ;;
  restart)
    echo "App restarted"
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    ;;
esac

โœ… Run:

./script.sh restart

โœ… Output:

App restarted

๐Ÿง  Why Use case Instead of if-elif-else?

Featurecaseif-elif-else
Syntax readabilityโœ… CleanerโŒ Verbose with many conditions
Pattern matchingโœ… Built-in globโŒ Manual with [[ ]]
Command-line parsingโœ… Idealโœ… Possible but less elegant

๐Ÿงฑ Advanced Patterns

You can use multiple matches with |:

case $input in
  y|Y|yes|YES)
    echo "You said yes."
    ;;
  n|N|no|NO)
    echo "You said no."
    ;;
  *)
    echo "Unknown input."
    ;;
esac

๐Ÿ“Œ Summary โ€“ Bash Case Statements

The case statement in Bash is your go-to tool for handling multiple input conditions clearly and efficiently. It’s a perfect fit for user menus, argument validation, file type checks, and automation logic.

๐Ÿ” Key Takeaways:

  • Use case, in, esac to check multiple input patterns
  • ;; separates each case block
  • * acts as a default (catch-all)
  • More readable and scalable than nested if statements

โš™๏ธ Real-world Uses:

  • Parsing script parameters
  • Creating interactive terminal menus
  • Handling file type conditions in automation scripts

โ“ FAQ โ€“ Bash Case Statements


โ“ Can I use regex in a Bash case statement?
โœ… Not full regex, but you can use glob patterns like *.txt, [0-9]*, etc.


โ“ What does ;; mean in a case statement?
โœ… It ends the current condition block and prevents fall-through to the next one.


โ“ Is case faster than if in Bash?
โœ… Slightly more efficient in matching many conditions and much more readable for multiple options.


โ“ Can I use variables inside patterns?
โŒ Not directly. Pattern matching must use literal values or glob-style patterns, not variable substitution.


โ“ How do I add a default action in a case statement?
โœ… Use *:

*)
  echo "Default case"
  ;;

Share Now :

Leave a Reply

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

Share

๐ŸŸข Bash: Case Statements (case, in, esac)

Or Copy Link

CONTENTS
Scroll to Top