1

I'm newbie in bash script, I want to create a switch case without "break" like java code below

switch(choice) {
    case 1: 
           System.out.println("1");
    case 2:
           System.out.println("2");
}

choice = 1

output: 1
        2

so ... how can I do that in bash script :(

1 Answer 1

2

Prior to bash 4, you couldn't; at most one block of commands in a case statement would be executed. bash 4 introduced the ;& case terminator, which causes fall-through.

case $choice in
    1) echo 1 ;&
    2) echo 2 ;;
esac
Sign up to request clarification or add additional context in comments.

5 Comments

@hoaithy92: if you run bash --version, is it version 3.x or 4.x?
@JonathanLeffler it version is 4.3.1
@hoaithy92: OK; that should support the caseesac notation that @chepner showed. The switch notation you showed doesn't apply to Bash; it might be valid in C shell, or something similar might be valid (I neither know nor care).
So that no way to do like switch that I showed, right ? :(
@hoaithy92: try calling your script with bash, maybe you had called it with sh or its shebang is /bin/sh instead of /bin/bash, and your /bin/sh wasn't linked to /bin/bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.