Reputation: 63062
Given the following bash loop:
for ((x=1; x<=$y; x++)); do echo $x; done
1
2
3
4
How to "background" the individual tasks?
09:25:58/~ $for ((x=1; x<=$y; x++)); do echo $xi &; done
-sh: syntax error near unexpected token `;'
I tried "bash"ing the echo and that did not work either:
09:26:37/~ $for ((x=1; x<=$y; x++)); do bash -c "echo $x" &; done
-sh: syntax error near unexpected token `;'
Upvotes: 8
Views: 3842
Reputation: 80931
Both &
and ;
are command terminators in the shell.
You only need to terminate each command once. So don't use both together:
for ((x=1; x<=$y; x++)); do echo $x & done
You would get the same error by using two ;
s as well:
$ for ((x=1; x<=$y; x++)); do echo $x ; ; done
-bash: syntax error near unexpected token `;'
Note that trying to use ;;
gets a different error because ;;
is a special token to the shell (used in case
statements):
-bash: syntax error near unexpected token `;;'
%start complete_command %% complete_command : list separator | list ; list : list separator_op and_or | and_or ; .... separator_op : '&' | ';' ; separator : separator_op linebreak | newline_list ;
Upvotes: 17
Reputation: 254
This will work:
for ((x=1; x<=y; x++)); do echo $x & done
Note that there is no ;
after &
. However using line breaks will make it more readable:
for ((x=1; x<=y; x++)); do
echo $x &
done
Upvotes: 2