case in bash: "line 4: syntax error near unexpected token `)'"

14,268

You are missing ;; at the end of each pattern:

#!/bin/bash
case "$1" in
        help)
            echo "You asked for help. Sorry, I'm busy."
            ;;
        *)
            echo "You didn't say anything. Try 'help' as the first argument."
            ;;
esac

Think of it as a break statement in a programming language. They are compulsory on case.

Share:
14,268
Eric Reed
Author by

Eric Reed

Sorry for the duplicate question. I design web and desktop applications! Need help or want to make a business offer? Contact me here: mrericcoolnessreed (at) gmail (dot) com I'm currently proficient in: Java (although I'll avoid it at all costs) C# (not a ton of experience) Python (lots of experience) JS+CSS+HTML (client-side, minimal server-side/Node work) PHP (tend to avoid) Obj-C/Swift (basic syntax/coding conventions, Xcode-less) C++ (preferred language) I'm currently learning: C internals

Updated on June 19, 2022

Comments

  • Eric Reed
    Eric Reed almost 2 years

    case in bash:

    line 4: syntax error near unexpected token `)'

    I'm trying to use the command case in Bash (on my Raspberry Pi again), but when I run my script, Bash spits out errors. I've read over many tutorials and I think I'm doing the same thing as them, but something's just not right.

    Here's my code:

    #!/bin/bash
    case "$1" in
            help) echo "You asked for help. Sorry, I'm busy."
            *) echo "You didn't say anything. Try 'help' as the first argument."
    esac
    

    Here's the output (the filename is newmkdir and I ran it with no arguments):

    ./newmkdir: line 4: syntax error near unexpected token `)'
    ./newmkdir: line 4: `   *) echo "You didn't say anything. Try 'help' as the first argument."'
    

    I'm trying to have my script interpret help and then make anything else output the next line.

    (Note this is just an example of a glitched script. This script has no meaning and might not even make sense, it's just a test.)

  • Jonathan Leffler
    Jonathan Leffler over 8 years
    Technically, the ;; is optional before the esac, but that's a bad reason for leaving it off.
  • Eric Reed
    Eric Reed over 8 years
    Thanks! ... I realized the tabbing 'issue' right after I posted this, but thanks anyway for correcting it.