Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Resetting a signal handler in Bash restores a signal’s disposition to its default kernel action (SIG_DFL) or clears internal shell hooks. This operation is performed using the trap builtin, which unbinds any custom command string previously associated with the specified signal. According to POSIX and Bash semantics, signals that are ignored upon entry to the shell cannot be trapped or reset; therefore, a reset operation practically applies only to signals that were not initially ignored.

Syntax

The POSIX-compliant and most explicit method to reset a signal handler is to pass a single hyphen (-) as the command argument, followed by one or more signal specifications:
trap - <signal_spec> [<signal_spec> ...]
Note: <signal_spec> can be a signal name (with or without the SIG prefix, e.g., INT or SIGINT), a signal number (e.g., 2), or a shell condition (e.g., EXIT). Alternatively, a trap can be reset by omitting the command argument entirely. If the first argument provided is a valid signal specification, Bash treats the action argument as absent and resets all provided arguments (including the first) as signals to reset:
trap <signal_spec> [<signal_spec> ...]
POSIX explicitly states that if the first operand is an unsigned decimal integer, the shell shall treat all operands as conditions to reset (e.g., trap 2 15). Bash extends this functionality to accept any valid signal name or shell condition as the first operand (e.g., trap INT TERM). In both cases, every argument passed is treated as a signal to be reset. Note: This behavior only applies when the first argument is a recognized signal specification. Valid single-argument options such as -p (print traps) or -l (list signals) are processed as flags and do not trigger a reset operation.

Mechanics and Behavior

1. Default Disposition Restoration

Bash maintains an internal table mapping signals to their dispositions. When a custom trap is set, Bash stores the provided command string. When the trap is reset, Bash clears this string from memory and reverts the signal’s handling to the kernel’s default action (SIG_DFL). Crucially, if the shell was invoked with a signal already ignored (e.g., running a script via nohup), that signal’s disposition is locked. Bash silently ignores any attempt to set a trap for an initially ignored signal. Consequently, a reset operation on such a signal is effectively a no-op, as the disposition was never changed from SIG_IGN in the first place.

# 1. Override disposition
trap 'cleanup_function' SIGTERM


# 2. Reset to default disposition (SIG_DFL) using the explicit hyphen
trap - SIGTERM


# 3. Reset multiple signals by omitting the action argument
trap INT TERM
trap 2 15

2. Resetting vs. Ignoring

Resetting a signal is fundamentally different from explicitly ignoring a signal.
  • Reset (trap - SIGINT): Restores the default kernel behavior (SIG_DFL).
  • Ignore (trap '' SIGINT): Explicitly sets the disposition to SIG_IGN. The signal is silently discarded by the kernel and never delivered to the process.
trap '' SIGINT  # Signal is explicitly ignored
trap - SIGINT   # Signal is reset; default behavior (SIG_DFL) is restored

3. POSIX Conditions and Non-POSIX Pseudo-Signals

Bash supports standard POSIX trap conditions (such as EXIT or 0) alongside non-POSIX pseudo-signals (shell extensions): ERR, DEBUG, and RETURN. Because these shell conditions and pseudo-signals do not exist at the kernel level, resetting them does not interact with kernel-level signal dispositions. Instead, resetting them simply removes the internal shell hook, meaning no action will be taken when the corresponding shell event occurs.
trap 'echo "Error occurred"' ERR
trap - ERR # Removes the ERR hook; errors will no longer trigger the echo

4. Subshell Inheritance

When Bash forks a subshell (e.g., via $(...) or (...)), the subshell inherits the signal dispositions of the parent with one critical exception: trapped signals are automatically reset to their default values (SIG_DFL). If a parent script traps SIGINT, the subshell will not execute the parent’s trap command upon receiving SIGINT; it will revert to the default disposition. However, signals that are explicitly ignored (trap '' SIG) in the parent remain ignored in the subshell.
Master Bash with Deep Grasping Methodology!Learn More