2

I was curious as to whether or not there is an equivalent to PowerShell's "Switch" command that lets you maneuver with input instead of using a multitude of "if-statements"

Paco R.
  • 21
  • 1

1 Answers1

11

If you're looking at a variable to determine flow, you want to use a case statement.

case "$var" in
   val1)
      do_something
      ;;
   val2)
      do_something_else
      ;;
esac

If you're looking to interactively get user input, you want to also use a select statement.

select action in proceed ponder perspire quit; do
    case "$action" in
        proceed)
           go_on_then
           ;;
        ponder)
           have_a_think
           ;;
        perspire)
           exude_salty_secretions
           ;;
        quit)
           break
           ;;
    esac
done
DopeGhoti
  • 73,792
  • 8
  • 97
  • 133
  • 2
    Worth noting that it's matching against glob patterns. For example, if you write `[Qq]uit|exit|go-*)`. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere". `default` would be `*)`, which is simply a glob that matches anything. – JoL Apr 19 '18 at 22:51