3 ms·
> Well, because it simply doesn't work for them: returning from a function does not trigger the EXIT signal. It doesn't trigger EXIT, but it does trigger RETUR
by pdkl95 5y ago
> Well, because it simply doesn't work for them: returning from a function does not trigger the EXIT signal.
It doesn't trigger EXIT, but it does trigger RETURN. Just trap both:
#!/bin/bash
foo() {
trap "echo 'Cleanup!'" RETURN EXIT
#return
#exit
echo "Kill me with ^C or \"kill $$\""
while true ; do : ; done
}
foo # should print 'Cleanup!' on SIGTERM,
# returning, or calling exit