User Tools

Site Tools


bash:switch

BASH - Switch

The switch construct can be used where nested conditions are required, but you don’t want to use complex if-else-elif chains.

#!/bin/bash
 
echo -n "Enter a number: "
read num
 
case $num in
100)
echo "Hundred!!" ;;
200)
echo "Double Hundred!!" ;;
*)
echo "Neither 100 nor 200" ;;
esac

NOTE: The conditions are written between the case and esac keywords.

The *) is used for matching all inputs other than 100 and 200.

bash/switch.txt · Last modified: 2021/01/09 15:55 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki