Skip to content

Instantly share code, notes, and snippets.

@CrBoy
Created March 17, 2012 14:46
Show Gist options
  • Save CrBoy/2060346 to your computer and use it in GitHub Desktop.
Save CrBoy/2060346 to your computer and use it in GitHub Desktop.
The `trap' in bash scripts
#!/bin/bash
trap "echo TRAP!!; exit" SIGTERM SIGINT SIGHUP
for (( i=0; i<5; i=i+1 ))
do
echo $i
sleep 1
done
#!/bin/bash
function clean_up {
echo CLEAN UP!!
exit
}
trap clean_up SIGTERM SIGINT SIGHUP
for (( i=0; i<5; i=i+1 ))
do
echo $i
sleep 1
done
clean_up
#!/bin/bash
function clean_up {
echo CLEAN UP!!
}
trap "clean_up;exit" SIGTERM SIGINT SIGHUP
for (( i=0; i<5; i=i+1 ))
do
echo $i
sleep 1
done
clean_up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment