129

I have a shell script which contains the following:

case $1 in
    0 )
    echo $1 = 0;
    OUTPUT=3;;
    1 )
    echo $1 = 1;
    OUTPUT=4;;
    2 )
    echo $1 = 2;
    OUTPUT=4;;
esac

HID=$2;
BUNCH=16;
LR=.008;

Are semicolons completely superfluous in the snippet above? And is there any reason for some people using double semicolons?

It appears semicolons are only a separator, something you would use instead of a new line.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Nagel
  • 2,576
  • 3
  • 22
  • 20

4 Answers4

183

Single semicolons at the end of a line are superfluous, since the newline is also a command separator. case specifically needs double semicolons at the end of the last command in each pattern block; see help case for details.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 26
    Great! So, if I understand you correctly, I can safely remove any single semicolons at the end of any line, but never double ones? – Nagel Sep 21 '11 at 22:14
  • You could use it as best practice because some popular programming languages like Java use this. So if you keep using this as best practice in bash, you will never forget this in Java. – MaXi32 Jun 29 '21 at 11:46
  • 2
    @MaXi32 I tried to upvote your comment using my teeth so that I don't forget how to eat :-) – Ardent Coder Nov 06 '21 at 13:14
36

According to man bash:

  metacharacter
         A character that, when unquoted, separates words.  One of the following:
         |  & ; ( ) < > space tab
  control operator
         A token that performs a control function.  It is one of the following symbols:
         || & && ; ;; ( ) | |& <newline>

So, the ; can be metacharacter or control operator, while the ;; is always a control operator (in case command).

In your particular code, all ; at the end of line are not needed. The ;; is needed however.

Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
  • 4
    What's the practical difference between `;` and `;;`, then? I'm not familiar enough with BASH syntax parsing to know the practical difference between what BASH calls a "metacharacter" and what it calls a "control operator". – jvriesem May 22 '18 at 20:00
  • 1
    agree with jvriesem's questions & comments, the docs snippet seems a bit too narrow – grgry Jul 11 '18 at 23:55
7

In the special case of find, ; is used to terminate commands invoked by -exec. See the answer of @kenorb to this question.

d-_-b
  • 21,536
  • 40
  • 150
  • 256
DanielGKiel
  • 71
  • 1
  • 1
1

@Opensourcebook-Amit

newlines equivalent to single semicolon ; on terminal or in shell script.

See the below examples:

On terminal:

[root@server test]# ls;pwd;

On shell script:

[root@server test]# cat test4.sh

echo "Current UserName:"
whoami

echo -e "\nCurrent Date:";date;

[root@server test]#

But I am not agree with the comment that & is equivalent to newline or single semicolon

& is run commands in background also a command separator but not worked as semicolon or newline.

d-_-b
  • 21,536
  • 40
  • 150
  • 256