Does .bashrc contain syntax errors?

8

1

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?

Argent

Posted 2019-03-16T23:57:17.080

Reputation: 253

11There'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 – 2019-03-17T07:43:11.183

4if it's an error then you'll get a report every time you open a new terminal – phuclv – 2019-03-17T08:42:04.150

14Prior 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 with Monica – 2019-03-17T16:37:32.550

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 – 2019-03-18T08:36:19.840

@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 – 2019-03-20T18:22:37.743

Answers

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

baelx

Posted 2019-03-16T23:57:17.080

Reputation: 2 083

(the information is also available from help case command.) – user202729 – 2019-03-17T10:38:30.447

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 – 2019-03-17T17:55:27.543

3

Not just in bash; the POSIX shell specification allows an optional opening ( as well.

– chepner – 2019-03-17T22:20:48.033