typestar

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

  1. Each arm is a pattern list ending in ).
  2. y|yes|Y alternates; globs like *.txt work too.
  3. * is the fallback, and ;; closes every arm.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Conditionals, step 5 of 26 in Language basics.

← Previous Next →