Bash special parameters #2
The trap defines and activates handlers to be run when the shell receives
signals or other special conditions.
trap [-lp] [[ARG] SIGNAL_SPEC...]-
ARGis a command to be read and executed when the shell receives the signal(s) -
SIGNAL_SPEC. IfARGis absent (and a singleSIGNAL_SPECis supplied) orARGis a dash (“-“), each specified signal is reset to its original value. IfARGis the null string, eachSIGNAL_SPECis ignored by the shell and by the commands it invokes. -
If a
SIGNAL_SPECisEXIT (0),ARGis executed upon exit from the shell. -
If a
SIGNAL_SPECisDEBUG,ARGis executed before every simple command. -
If a
SIGNAL_SPECisRETURN,ARGis executed each time a shell function or a script run by the “.” or source built-in commands finishes executing.
A SIGNAL_SPEC of ERR means to execute ARG each time a command’s failure
would cause the shell to exit when the -e option is enabled. If no arguments
are supplied, trap prints the list of commands associated with each signal.
In bash $$ is the process ID, which is fine if you need a single file,
otherwise take a look at the mktemp command.
Example extracted from .git_template/hooks/ctags of my dotfiles:
1
2
3
4
5
6
7
set -e
PATH="/usr/local/bin:$PATH"
dir="`git rev-parse --git-dir`"
trap 'rm -f "$dir/$$.tags"' EXIT
git ls-files | \
ctags --tag-relative -L - -f"$dir/$$.tags" --languages=-javascript,sql
mv "$dir/$$.tags" "$dir/tags"
Explanation for git command: git rev-parse --git-dir
Show $GIT_DIR if defined. Otherwise show the path to the .git directory.
The path shown, when relative, is relative to the current working directory.
If $GIT_DIR is not defined and the current directory is not detected to lie
in a Git repository or work tree print a message to stderr and exit with
nonzero status.