Break for case statement

2.5k Views Asked by At

How can I prevent to run *) (with echo "Other") in follow code?

I want when $a is equal 18 just echo "A". Now echo "A" and "Other"

case "$a" in

[17-20])  
    echo "A"
    ;;
*) echo "Other"
   ;;
esac
2

There are 2 best solutions below

0
On BEST ANSWER

You can use this:

case "$a" in
  1[789])      # range 17-19
    echo "A"
    ;;
  20)          # only 20
    echo "A"
    ;;
  *)
    echo "Other"
    ;;
esac

Shorter version:

case "$a" in
  1[7-9]|20)      # range 17-19 and 20
    echo "A"
    ;;
  *)
    echo "Other"
    ;;
esac
0
On

case doesn't know about ranges and only takes patterns.

Instead you could use if ... else.

if ((a>=17 && a<=20)); then
  echo "A"
else
  echo "Other"
fi