Trap 'Ctrl + c' for bash script but not for process open in this script
You should use trap true 2
or trap : 2
instead of trap '' 2
. That's what "help trap" in a bash shell says about it:
If ARG is the null string each SIGNAL_SPEC is ignored by the shell and by the commands it invokes.
Example:
$ cat /tmp/test
#! /bin/sh
trap : INT
cat
echo first cat killed
cat
echo second cat killed
echo done
$ /tmp/test
<press control-C>
^Cfirst cat killed
<press control-C>
^Csecond cat killed
done
You can reset a trap to its default by giving the trap command -
as its action argument. If you do this in a subshell, it won't affect the trap in the parent shell. In your script, you can do this for each command that you need to be interruptible with Ctrl-C:
#!/bin/bash
# make the shell (and its children) ignore SIGINT
trap '' INT
.
.
.
# but this child won't ignore SIGINT
(trap - INT; my_program)
# the rest of the script is still ignoring SIGINT
.
.
.