Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand not working inside for loop in shell script

Tags:

shell

I need to execute

node simulator.js 1 &
node simulator.js 2 &
node simulator.js 3 &
node simulator.js 4 &
node simulator.js 5 &
...
node simulator.js 10 &

So, I was trying out the shell script for loop to do this, but I am getting this error.

user@host$ for i in {1..10}; do node simulator.js "${i}" &; done
bash: syntax error near unexpected token `;'

I am pretty new to Shell scripts, might be a very small thing here, can someone help figure it out?

UPDATE

the issue with not with the for loop, its with the &, the error persists even if I do

for i in {1..10}; do node simulator.js 1 &; done
like image 770
Parthapratim Neog Avatar asked Nov 28 '25 22:11

Parthapratim Neog


1 Answers

Problem is semi-colon after &.

This should work

for i in {1..10}; do node simulator.js 1 & done

This will fork 10 subshell processes.

like image 76
anubhava Avatar answered Nov 30 '25 14:11

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!