Trap rm command

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

You can trap the rm command command with the following syntax:

#!/bin/bash
OUTPUT=/tmp/output.$$

# trap the rm command if CTRL+C or other signal occurred 
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM

# do something
...
..

# try to remove file
rm -f $OUTPUT

See also