The case name in the shell under Linux is similar to the switch in C + +, but cases in the shell are more powerful and complex.
1, strong mainly reflected in: Shell in case the keyword can be a string type, and each item can contain regular expressions.
2, the complexity is mainly reflected in: Shell case in each item has three choices: Break (normal break), unconditional follow up (unconditional continuation) and conditional follow up (conditional continue).
The focus of this article is on the 2nd above.
The unconditional continuation and conditional continuation of a case is a feature in Bash 4.1.x and later.
Perform the following command to view your current bash version
Echo $BASH _version
A regular break is added after each item;;
To continue unconditionally is to add;& after each item
There are conditions for the continuation of each of the following add;; &
Test code
#!/bin/bashecho "Test the most ..." case "1" in 1) echo ' 1 ' ;; & 2) echo ' 2 ' ;; 3) echo ' 3 ' ;; ?) echo '? ' ;; *) Echo ' * ' ;; Esacecho "Test No.2 ..." case "1" in 1) echo ' 1 ' ;; 2) echo ' 2 ' ;; & 3) echo ' 3 ' ;; ?) echo '? ' ;; *) Echo ' * ' ;; Esacecho "Test No.3 ..." case "1" in 1) echo ' 1 ' ;& 2) echo ' 2 ' ;; 3) echo ' 3 ' ;; ?) echo '? ' ;; *) Echo ' * ' ;; Esacecho "Test No.4 ..." case "1" in 1) echo ' 1 ' ;; 2) echo ' 2 ' ;& 3) echo ' 3 ' ;; ?) echo '? ' ;; *) Echo ' * ' ;; Esac
Test results:
Test ... 1? Test No.2 ... 1Test No.3 ... 12Test No.4 ... 1
Description of case command in Shell under Linux (regular break, unconditional continuation and conditional continuation)