Why trap works differently with bash than with sh?

65 Views Asked by At

I have small script that sets trap and tries to send SIGINT to the child when it receives that signal itself.

However, it doesn't seem to work with sh at all, unlike with bash.

#!/bin/bash

first() {
    trap 'echo "INT first"' INT
    sleep 10
    echo "slept"
}

trap 'echo "killing"; kill -INT "$PID"; echo "kill sent"' INT

first &
PID=$!
echo "$PID"

wait
$ ./test.sh 
64257
^Ckilling
kill sent
INT first
slept

After I change shebang to #!/bin/sh the child doesn't seem to catch the signal.

$ ./test.sh 
64550
^Ckilling
kill sent

String "slept" will be printed after 10 second sleep.

0

There are 0 best solutions below