title: How to handle interrupt signal when reading from stdin in bash
body: <p>I'm playing around with bash read functionality. I like what I have so far as a simple layer on top of my current shell. The read -e does tab-complete and previous commands, and sending EOF with ctrl+d gets me back to my original shell. Here's my reference:</p>
  
  <p><a href="https://stackoverflow.com/questions/20525056/bash-or-other-shell-wrap-all-commands-with-function-script">Bash (or other shell): wrap all commands with function/script</a></p>
  
  <p>I'd like some help handling SIGINT, ctrl+c. In a normal shell, if you start typing and hit ^C halfway, it immediately ends the line. For this simple example, after ^C, I still have to hit return before it's registered. </p>
  
  <p>How do I keep the nice things that readline does, but still handle SIGINT correctly? Ideally, it would send a continue statement to the while read loop, or somehow send a \n to STDIN where my read is waiting.</p>
  
  <p>Example code:</p>
  
  <pre><code>#!/bin/bash
  # Emulate bash shell
  
  gtg=1
  
  function handleCtrl-C {
      # What do I do here?
      gtg=0
      return
  }
  
  trap handleCtrl-C INT
  
  while read -e -p "&gt; " line
  do
      if [[ $gtg == 1 ]] ; then
          eval "$line"
      fi
      gtg=1
  done
  </code></pre>