12

Why is this?

if true; then sleep 3 &; fi
bash: syntax error near unexpected token `;'

I want to run

sleep 3

in the background so that the command ["sleep 3" is just an example] would run in "paralell" style, so it finishes faster. But I'm getting this:

bash: syntax error near unexpected token `;'

error message. Why? Why can't I send a task to the background?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
LanceBaynes
  • 39,295
  • 97
  • 250
  • 349

3 Answers3

14

It is because & is already a statement separator, so you should not put ; after this.

enzotib
  • 50,671
  • 14
  • 120
  • 105
6

Seems like you don't need to separate commands in that case (& separated them itself).

For example.

$> if true; then (sleep 3; echo ok) &  fi
[1] 14224
$> ok
  • This works but spawns a subshell, i.e. an extra process (which extra memory burden, and variable settings you do inside the `( ... )` will not be visible outside) – Golar Ramblar May 31 '21 at 17:14
0

Try to write it multiline:

if true; then
  sleep 3 &
fi
Golar Ramblar
  • 1,771
  • 1
  • 18
  • 28