Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What does ; mean in single line scripts like this:

while true; do sudo -n true; sleep 60; kill -0 '$$' || exit; done 2>/dev/null &

Does it mean new line, or "next command"?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It's a separator of commands. Though in the first instance, it might be better to think of it as ending the while statement.

For example, if you wanted to do a loop while some command returns success, you would do something like

while test -f /foo; do some_command; done`

The semicolon is used to indicate the end of the arguments to test. Otherwise it would think that do is another argument to test.

However you can use newlines instead of the semicolon. The following would be exactly equivalent to the example above, just without any semicolons

while test -f /foo
do
some_command
done

In fact with bash, if you run the above command, and then after it finishes (or you CTRL+C it), if you go back in history (up arrow keys or whatnot), you'll notice it replaces the multi-line command with one using semicolons instead.


So yes, the syntax for things like if and while break normal shell behavior.
I've personally always thought the syntax is weird, as the do looks strange. But you get used to it.

share|improve this answer
    
Now it makes sense. Thank you so much. –  Danijel J Oct 5 '13 at 6:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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