title: How do I trap SIGQUIT properly in a bash script?
body: <p>I can write shell scripts that trap <code>SIGINT</code> just fine, but I can't seem to trap <code>SIGQUIT</code>.</p>
  
  <pre><code>#!/bin/bash
  
  function die {
      echo "Dying on signal $1"
      exit 0
  }
  
  trap 'die "SIGINT"' SIGINT
  trap 'die "SIGQUIT"' SIGQUIT
  
  while true; do
      echo "sleeping..."
      sleep 5
  done
  </code></pre>
  
  <p>Executing this script and pressing <code>CTRL-C</code> has the desired effect, but pressing <code>CTRL-\</code> (which, as I understand, should trigger <code>SIGQUIT</code>) does nothing except print <code>^\</code> in the terminal. Why?</p>
  
  <p>I have two running theories. The first is that the semantics of <code>SIGINT</code> and <code>SIGQUIT</code> are different such that <code>SIGQUIT</code> only gets sent to the child process <code>sleep</code>, while <code>SIGINT</code> gets sent to both the child process and the parent bash process. If this is the case, where is it documented?</p>
  
  <p>My second theory is that bash not only ignores (i.e., has a no-op handler for) <code>SIGQUIT</code> by default (as the man page suggests), but does not allow it to be trapped at all. This theory overlaps with the first theory, since it could be the case that <code>SIGQUIT</code> is going to both parent and child, but the parent (<code>bash</code>) just can't trap it. If this is the case, is there <i>any</i> way to trap <code>SIGQUIT</code> in a bash script?... perhaps some <code>shopt</code> I can set?</p>
  
  <p>Edit: this is on Ubuntu 10.10 in gnome-terminal 2.32.0 running bash 4.1.5, and yes <code>^\</code> is configured to issue SIGQUIT (as reported by <code>stty -a</code> and confirmed by issuing <code>^\</code> SIGQUITs to other programs like <code>ping</code>).</p>
  
  <p>UPDATE:
  I just discovered that the problem must be due somehow to gnome-terminal. If I run this script from a virtual console (i.e., <code>ctrl-alt-f1</code> to get out of X), it traps SIGQUIT perfectly fine when I press <code>^\</code>. Same bash and everything, so the only difference must be the terminal emulator. So now my question becomes: how can I configure gnome-terminal to behave like the virtual console in this respect? I <code>diff</code>'d the outputs of <code>stty -a</code> in the virtual console and in gnome-terminal, and while there are differences, nothing seems immediately relevant (e.g., they both have <code>quit = ^\;</code>).</p>
  
  <p>UPDATE 2:
  Another experiment. Simply execute <code>$ sleep 60</code> in gnome-terminal; press <code>^\</code> and the signal goes uncaught. Now execute <code>$ sleep 60</code> in the virtual console; press <code>^\</code> and the signal <i>is</i> caught -- the process prints <code>Quit</code> and exits. But now run <code>$ ping google.com</code> in gnome-terminal and press <code>^\</code> -- the signal is caught and handled as expected. So there is something weird about gnome-terminal such that SIGQUIT can be caught by some programs, but not by others, even if those other others <i>do</i> catch it when invoked from the virtual console. Perhaps I should just upgrade my gnome-terminal.</p>