考虑:
$cat b.sh #!/bin/bash trap 'echo $$was interrupted' INT read foo echo done $./b.sh ^C27104 was interrupted ^C27104 was interrupted ^C27104 was interrupted done $
(ctrl-c被击中3次,然后是ctrl-d)
我希望执行陷阱后读取中止.是否有一种干净的方法来实现这一目标?
似乎没有立即中断是Bash非POSIX扩展(请参阅Bash内置源的read.def中的read_builtin(查找posixly_correct)).
您可以覆盖此行为,并通过强制读取POSIX行为(通过设置POSIXLY_CORRECT环境变量)退出第一个Ctrl C:
#!/bin/bash trap 'echo $$was interrupted' INT POSIXLY_CORRECT=1 read foo echo done