Description of case command in Shell under Linux (regular break, unconditional continuation and conditional continuation)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.