Dash double semicolon (;;) syntax

SyntaxShDash Shell

Syntax Problem Overview


I'm trying to find a way to run multiple commands in parallel in sh and wait for it completion.

I've found that following doesn't work (sh: 1: Syntax error: ";" unexpected):

sh -c '(sleep 3 && echo 1) & ; (sleep 3 && echo 2) & ;  wait'

But this syntax works as expected:

sh -c '(sleep 3 && echo 1) & ;; (sleep 3 && echo 2) & ;;  wait'

But I don't understand what is the difference.

What is the meaning of ;; and when it should be used?

Syntax Solutions


Solution 1 - Syntax

;; is only used in case constructs, to indicate the end of an alternative. (It's present where you have break in C.)

case $answer in
  yes) echo 'yay!';;
  no) echo 'boo!';;
esac

Syntactically, ; and & both mark the end of a command. A newline is equivalent to ;, in a first approximation. The difference between them is that ; or newline indicates that the command must be executed in the foreground, whereas & indicates that the command must be executed in the background.

So here you need & wait. & ; is a syntax error (you can't have an empty command). & ;; is also a syntax error; ash lets it go (as if you'd written just &), but bash complains. Evidently your sh is some ash variant (such as dash, which is /bin/sh on many Debian derivatives).

Solution 2 - Syntax

It should be used in a case statement, between cases. The issue you're having here is that both & and ; are command separators, and you should only be using one of them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvalodzkaView Question on Stackoverflow
Solution 1 - SyntaxGilles 'SO- stop being evil'View Answer on Stackoverflow
Solution 2 - SyntaxIgnacio Vazquez-AbramsView Answer on Stackoverflow