bash 循环后台作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12472933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:43:18  来源:igfitidea点击:

Loop background job

bash

提问by bougui

I try to run a background job in a for loop in bash:

我尝试在 bash 的 for 循环中运行后台作业:

for i in $(seq 3); do echo $i ; sleep 2 & ; done

I get error:

我得到错误:

bash: syntax error near unexpected token `;'

In zsh the command line works.

在 zsh 中,命令行有效。

回答by gammay

Remove the ; after sleep

去除那个 ; 睡后

for i in $(seq 3); do echo $i ; sleep 2 & done

BTW, such loops are better written on separate lines with proper indentation (if you are writing this in a shell script file).

顺便说一句,这样的循环最好用适当的缩进写在单独的行上(如果你在 shell 脚本文件中写这个)。

for i in $(seq 3)
do
   echo $i
   sleep 2 &
done

回答by sogart

You can put the background command in ( )

您可以将后台命令放在 ( )

for i in $(seq 3); do echo $i ; (sleep 2 &) ; done