#!/bin/bash touch /tmp/trap.$$ trap 'echo "trapped"; rm /tmp/trap.$$; exit 1' 2 while : do echo "loop" sleep 10 done rm /tmp/trap.$$