case in Bash
One value against many patterns, first match wins.
answer="yes"
case $answer in
y|yes|Y)
echo "confirmed"
;;
n|no|N)
echo "declined"
;;
*.txt)
echo "that is a filename, not an answer"
;;
*)
echo "unrecognized: $answer"
;;
esac
How it works
- Each arm is a pattern list ending in
). y|yes|Yalternates; globs like*.txtwork too.*is the fallback, and;;closes every arm.
Keywords and builtins used here
caseechoesacin
The run, in numbers
- Lines
- 16
- Characters to type
- 176
- Tokens
- 42
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 28 seconds.
Step 2 of 3 in Conditionals, step 5 of 26 in Language basics.