title: Bash, CTRL+C in eval not interrupting the main script
body: <p>In my bash script, I'm running an external command that's stored in <code>$cmd</code> variable. (It could be anything, even some simple bash oneliner.)</p>
  
  <p>If <kbd>ctrl</kbd>+<kbd>C</kbd> is pressed while running the script, I want it to kill the currently running <code>$cmd</code> but it should still continue running the main script. However, I would like to preserve the option to kill the main script with <kbd>ctrl</kbd>+<kbd>C</kbd> when the main script is running.</p>
  
  <pre><code>#!/bin/bash
  cmd='read -p "Ooook?" something; echo $something; sleep 4 ' 
  while true; do
      echo "running cmd.."
      eval "$cmd"     # ctrl-C now should terminate the eval and print "done cmd"
      echo "done cmd"
      sleep 5         # ctrl-C now should terminate the main script
  done
  </code></pre>
  
  <p>Any idea how to do it some nice bash way?</p>
  
  <p>Changes applied based on answers:</p>
  
  <pre><code>#! /bin/bash
  
  cmd='read -p "Ooook1?" something; read -p "Oook2?" ; echo $something; sleep 4 ' 
  while true; do
      echo "running cmd.."
      trap "echo Interrupted" INT
      eval "($cmd)"     # ctrl-C now should terminate the eval and print "done cmd"
      trap - INT
      echo "done cmd"
      sleep 5         # ctrl-C now should terminate the main script
  done
  </code></pre>
  
  <p>Now, pressing <kbd>ctrl</kbd>+<kbd>C</kbd> while "Ooook1?" read will break the eval only after that read is done. (it will interrupt just before "Oook2") However it will interrupt "sleep 4" instantly. </p>
  
  <p>In both cases it will do the right thing - it will just interrupt the eval subshell, so we're almost there - just that weird read behaviour..</p>