|
A Syntax breakdown:
|
|
|
Output:
It's an apple |
|
|
A |
|
|
Output:
Color is blue |
|
|
The |
|
|
Output:
Unknown animal: cat |
|
|
Use |
|
|
Output:
It's a weekend |
|
|
Case patterns support glob-style wildcards.
Use |
|
|
Output:
script.sh is a shell script |
|
|
Character classes |
|
|
Output:
'7' is a digit |
|
|
Case patterns are case-sensitive by default.
Use |
|
|
Output:
User said yes |
|
|
Sometimes you want to explicitly do nothing for
certain patterns. Use an empty block or just |
|
|
Output:
Logging complete |
|
|
Case statements are commonly used for subcommand dispatch. This pattern appears in init scripts and CLI tools that support commands like “start”, “stop”, “restart”. |
|
|
Output:
Service is running |
|
|
Bash
Bash extends case with fall-through operators:
- These are Bash-specific and not POSIX-compliant. |
|
|
Output:
Using ;& (unconditional fall-through): Good job! You passed. Using ;;& (continue testing patterns): Matched: hello Matched: h* Matched: *o |
|