Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I received the error:

bash: syntax error near unexpected token `;'

due to the following command:

evince foo.pdf bar.pdf &; emacs foo.tex &

I is it illegal to separate commands with ; when using & to background a job? Or is there another reason this didn't work?

Thank you.

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

You don't need the semicolon. After it's sent to the background it's free to get another command.

evince foo.pdf bar.pdf & emacs foo.tex &
share|improve this answer
    
This helped me with an if statement after 2h of swearing. If anyone should need this: if [ $(ps -ef |grep -c '[p]grep') -eq 0 ]; then nohup sleep 5 > /dev/null 2>&1</dev/null & fi –  Oktav Oct 15 '13 at 13:07
add comment

BTW, the core issue is that (Bourne-derived) shells do not allow empty commands.

";" and "&" are command terminators, meaning fg and bg respectively. So "; ;" (or ";" at the beginning of a line) is also invalid.

(Newline(s) imply ";" if there is a not-yet-terminated command, unless you use "\" to continue the line.)

Languages vary a lot on these policies:

C-derived languages allow empty statements.

Pascal and PERL have separators, not terminators.

share|improve this answer
add comment

No, it's just confused and can't work out quite what you mean.

You need to group the & with the command that you want to put in the background:

$ (evince foo.pdf bar.pdf &); emacs foo.tex &

That works fine. Even more explicit would be:

$ (evince foo.pdf bar.pdf &); (emacs foo.tex &)

Especially if you then wanted to chain more commands after the end as well.

share|improve this answer
    
Thanks for the input. I think I see why it balked. –  KennyPeanuts Apr 11 '11 at 14:42
    
There's no need to spawn a subshell just to justify the use of an unnecessary semi-colon. Parentheses aren't just syntax; they cause extra work to be done. –  chepner Jun 8 '12 at 15:24
add comment

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.