#!/bin/bash
signal=SIGUSR1

on_signal()
{
	echo "trapped $signal"
}
trap on_signal "$signal"

# Start "sleep 5" child process
$(which sleep) 5 &
childpid=$!
echo "started child process"

# Wait for child process to exit, but also handle "wait" returning due to signal.
# "wait" returns immediately with exit code 128+N if signal N is received,
# see section "SIGNALS" in the bash man page.
result=
while [[ -z "$result" || "$result" -gt 128 ]]; do
	trap on_signal "$signal"
	echo "wait"
	wait "$childpid"
	result=$?
	echo "wait returned exit code $result"
done

echo "done"
