Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dash double semicolon (;;) syntax

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?

like image 802
valodzka Avatar asked Jun 03 '13 20:06

valodzka


People also ask

How do you use a double semicolon?

Rules for Using SemicolonsUse a semicolon between two independent clauses that are connected by conjunctive adverbs or transitional phrases.

What is double semicolon?

A "double semicolon" does not have any special meaning in c. The second semicolon simply terminates an empty statement. So you can simply remove it.

Can I use two semicolons in one sentence?

Yes, semicolons can be used to connect three, or more, related independent clauses.

What is double colon in bash?

It's nothing, these colons are part of the command names apparently. You can verify yourself by creating and running a command with : in the name. The shell by default will autoescape them and its all perfectly legal. Follow this answer to receive notifications.


1 Answers

;; 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).

like image 169
Gilles 'SO- stop being evil' Avatar answered Sep 20 '22 14:09

Gilles 'SO- stop being evil'