8

In the Ubuntu 18.04 LT .bashrc file there is the following:

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

Isn't xterm-color) an instance of unbalanced parentheses? And why does the line end with two semicolons?

To be clear, this is not something I wrote. It's in the virgin file, not edited by me.

If there are syntax errors, to whom should I report this?

5
  • 11
    There's an easy way to check if this is a syntax error: just run it, and Bash will tell you whether it is a syntax error. In fact, this will be run every time you open a terminal, so you just have to look if there is a syntax error printed every time you open the terminal. – Jörg W Mittag Mar 17 '19 at 7:43
  • 4
    if it's an error then you'll get a report every time you open a new terminal – phuclv Mar 17 '19 at 8:42
  • 14
    Prior research might have included simply looking up the syntax for switch/case in Bash, through which you would have quickly discovered that this is entirely normal. – Lightness Races in Orbit Mar 17 '19 at 16:37
  • 6
    @JörgWMittag If you don't know what a script does, running it might be a not so clever idea. Checking it with bash -n .bashrc is probably better – ChatterOne Mar 18 '19 at 8:36
  • @ChatterOne: It gets executed every single time you open a terminal; if there is something malicious about it, it will already have happened a long time ago. – Jörg W Mittag Mar 20 '19 at 18:22
38

This is the standard, correct syntax for a bash case statement(known abstractly as a switch statement in general programming), albeit perhaps an odd syntax when compared to C, Java, or other languages.

From The Linux Documentation Project:

Nested if statements might be nice, but as soon as you are confronted with a couple of different possible actions to take, they tend to confuse. For the more complex conditionals, use the case syntax:

case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN) COMMAND-LIST;; esac
3
  • (the information is also available from help case command.) – user202729 Mar 17 '19 at 10:38
  • 4
    There can actually be an optional opening parenthesis in front of any of the patterns, i.e. case $var in (foo) echo something;; esac. Bash's command line help doesn't mention that, but the online reference manual has it (Along with an example case statement, too...) – ilkkachu Mar 17 '19 at 17:55
  • 3
    Not just in bash; the POSIX shell specification allows an optional opening ( as well. – chepner Mar 17 '19 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.