在 sh 中强行中断 for 循环

Zim*_*bby 8 bash zsh sh

如果我在 sh 的命令行上运行 for 循环,然后按 control-C,它通常会取消当前正在运行的进程,因此我需要按住 ^C 直到 shell 本身捕获它并中断循环。有没有办法立即中断当前进程循环?

Sha*_*hin 12

我知道的最简单的方法是挂起前台作业 ( ^Z),然后使用作业 ID ( kill %<JOB_ID>)终止它。

例子:

[me@host]$ while [ : ]; do less /etc/motd; done   # Ctrl-C can't kill this
Run Code Online (Sandbox Code Playgroud)

一个Ctrl+ 后z

[1]+  Stopped                 less /etc/motd

[me@host]$ kill %1
[me@host]$
Run Code Online (Sandbox Code Playgroud)

[1]暂停消息开头括号 ( ) 中的数字为您提供作业 ID。

您还可以使用该jobs命令列出挂起作业的 ID 。