O'Reilly logo

Linux Shell Scripting Essentials by Sinny Kumari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Switch to my choice

Switch is used to jump and run a certain case as per the result of the condition or expression is evaluated. It acts as an alternative to using multiple if in bash and keeps bash script much clear and readable.

The syntax of switch is as follows:

case $variable in
  pattern1)
  # Tasks to be executed
  ;;
  pattern2)
  # Tasks to be executed
  ;;
  …
  pattern n)
  # Tasks to be executed
  ;;
  *)
esac

In syntax, $variable is the expression or value that needs to be matched among the list of choices provided.

In each choice, a pattern or a combination of patterns can be specified. The ;; tells bash that end of given choice block. The esac keyword specify end of case block.

The following is an example to count the number of files and directories in a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required