Ampersand not working inside for loop in shell scr

2020-04-16 01:23发布

问题:

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

回答1:

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.



标签: shell