I can setup trap
in bash scripts but not bash functions.
For example, source
the code below and run f
, now try Ctrl+C, it's not been trapped!
How to setup trap
in bash functions?
4 trap ctrl_c INT
5
6 ctrl_c () {
7 echo "** Trapped CTRL-C"
8 }
9
10
11 f () {
12 for i in `seq 1 100`; do
13 sleep 1
14 echo -n "."
15 done
16 }
f
at the end of your code, and removing the space from thef ()
.