ss64.com Open in urlscan Pro
216.92.186.205  Public Scan

URL: https://ss64.com/bash/trap.html
Submission: On February 02 via api from IN — Scanned from DE

Form analysis 1 forms found in the DOM

GET https://www.google.com/search

<form action="https://www.google.com/search" method="get">
  <input type="text" name="q" id="qu" size="27" maxlength="255" aria-label="Search text">
  <input class="submit mousetrap" value="Search" id="btn" type="submit">
  <input type="hidden" name="sitesearch" value="ss64.com/bash/">
</form>

Text Content

WE VALUE YOUR PRIVACY

We and our partners store and/or access information on a device, such as cookies
and process personal data, such as unique identifiers and standard information
sent by a device for personalised ads and content, ad and content measurement,
and audience insights, as well as to develop and improve products. With your
permission we and our partners may use precise geolocation data and
identification through device scanning. You may click to consent to our and our
partners’ processing as described above. Alternatively you may click to refuse
to consent or access more detailed information and change your preferences
before consenting. Please note that some processing of your personal data may
not require your consent, but you have a right to object to such processing.
Your preferences will apply to this website only. You can change your
preferences at any time by returning to this site or visit our privacy policy.
MORE OPTIONSDISAGREEAGREE
 * SS64
 * Linux
 * How-to
 * 


TRAP

Execute a command when the shell receives a signal.

Syntax
      trap [-lp] [[arg] sigspec ...]

Key
   -p   Display signal commands (or trap commands for sigspec)

   -l   Print a list of signal names and their corresponding numbers.

   arg  The command to be executed when the shell receives signal(s) sigspec.


If arg is absent (and there is a single sigspec) or -, each specified signal is
reset to its original disposition (the value it had upon entrance to the shell).

If arg is the null string the signal specified by each sigspec is ignored by the
shell and by the commands it invokes.

If arg is not present and -p has been supplied, then the trap commands
associated with each sigspec are displayed. If no arguments are supplied or if
only -p is given, trap prints the list of commands associated with each signal.

Each sigspec is either a signal name, or a signal number. Signal names are case
insensitive and the SIG prefix is optional.

Number SIG     Meaning
0     0        On exit from shell
1     SIGHUP   Clean tidyup
2     SIGINt   Interrupt (CTRL-C)
3     SIGQUIT  Quit
6     SIGABRT  Cancel
9     SIGKILL  Die Now (cannot be trap'ped)
14    SIGALRM  Alarm Clock
15    SIGTERM  Terminate

If a sigspec is EXIT (0) the command arg is executed on exit from the shell.
If a sigspec is DEBUG, the command arg is executed before every simple command,
for command, case command, select command, every arithmetic for command, and
before the first command executes in a shell function (see SHELL GRAMMAR). Refer
to the description of the extdebug option to the shopt builtin for details of
its effect on the DEBUG trap.

If a sigspec is ERR, the command arg is executed whenever a simple command has a
non-zero exit status, subject to the following conditions. The ERR trap is not
executed if the failed command is part of the command list immediately following
a while or until keyword, part of the test in an if statement, part of a && or
|| list, or if the command's return value is being inverted via !.
These are the same conditions obeyed by the errexit option.

If a sigspec is RETURN, the command arg is executed each time a shell function
or a script executed with the . or source builtins finishes executing.

Signals ignored upon entry to the shell cannot be trapped or reset.
Trapped signals that are not being ignored are reset to their original values in
a child process when it is created.

When Bash receives a signal for which a trap has been set while waiting for a
command to complete, the trap will not be executed until the command completes.
When Bash is waiting for an asynchronous command via the wait built-in, the
reception of a signal for which a trap has been set will cause the wait built-in
to return immediately with an exit status greater than 128, immediately after
which the trap is executed.

The return status is False if any sigspec is invalid; otherwise trap returns
True.

This is a BASH shell builtin, to display your local syntax from the bash prompt
type: help trap


EXAMPLES

#!/bin/bash

$ SCRATCH=$(mktemp -t tmp.XXXXXXXXXX)

trap cleanup 1 2 3 6

cleanup()
{
  echo "Cleaning up temp files:"
  rm -rf "$SCRATCH"
  exit 1
}

### continue main script below


“When elephant steps on a trap, no more trap” ~ African Proverb


RELATED LINUX COMMANDS

exec - Execute a command.

Powered by pixfutureⓘ


--------------------------------------------------------------------------------

 
Copyright © 1999-2023 SS64.com
Some rights reserved