Bash

Bash:兩次發出陷阱

  • September 1, 2021

trap如果我兩次發出內置

$$ for the same signal $$, 發生什麼了?第二個命令是添加到第一個命令還是替換第一個命令?

trap Foo SIGINT
...
trap Bar SIGINT
...

當 SIGINT 發生時,Bash 只是執行Bar,還是也執行Foo?或者是其他東西…?

命令被替換。

手冊頁指出:

trap [-lp] [[arg] sigspec ...]
       The  command  arg  is  to  be  read  and executed when the shell
       receives signal(s) sigspec.  If arg is absent (and  there  is  a
       single  sigspec)  or  -,  each  specified signal is reset to its
       original disposition (the value it  had  upon  entrance  to  the
       shell).   If arg is the null string the signal specified by each
       sigspec is ignored by the shell and by the commands it  invokes.
       If  arg  is  not present and -p has been supplied, then the trap
       commands associated with each  sigspec  are  displayed.   If  no
       arguments  are  supplied or if only -p is given, trap prints the
       list of commands associated with each  signal.   The  -l  option
       causes  the shell to print a list of signal names and their cor‐
       responding numbers.   Each  sigspec  is  either  a  signal  name
       defined  in  <signal.h>,  or  a signal number.  Signal names are
       case insensitive and the SIG prefix is optional.

它說明了the command arg is to be read and executed ...時期。如果始終將 arg 添加到列表中,則無法重置信號處理。

引用自:https://unix.stackexchange.com/questions/170409