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.

In Bash, ignoring a signal is the process of instructing the shell to discard specific asynchronous POSIX signals (such as SIGINT or SIGTERM) rather than executing their default disposition. This is achieved using the trap builtin command by binding an empty string ('' or "") as the command argument for the target signal.

Syntax

trap '' SIGNAL_SPEC [SIGNAL_SPEC ...]
  • '' (Empty String): The exact mechanism that instructs the shell to ignore the signal. Note that if the command argument is omitted but a SIGNAL_SPEC is provided (e.g., trap SIGINT), Bash resets the signal to its default disposition. To print the current signal handlers, all arguments must be omitted (i.e., invoking just trap or trap -p).
  • SIGNAL_SPEC: The signal to be ignored. This can be the signal name with the SIG prefix (e.g., SIGINT), the signal name without the prefix (e.g., INT), or the numeric signal value (e.g., 2).

Implementation Examples


# Ignore SIGINT 
trap '' SIGINT


# Ignore multiple signals simultaneously (SIGTERM and SIGHUP)
trap '' TERM HUP


# Ignore signals using their numeric values
trap '' 2 15

Technical Characteristics

  • Unignorable Signals: The POSIX standard dictates that SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. Attempting to trap these in Bash will result in an error.
  • Inheritance of Ignored Signals on Entry: Per POSIX and Bash rules, signals that are already ignored upon entry to the shell environment cannot be trapped, caught, or reset to their default disposition by the script. Any attempt to change the disposition of a signal that was ignored at startup is silently discarded.
  • Child Process Inheritance: When a signal is explicitly ignored in a Bash script using trap '' SIGNAL, all child processes and subshells spawned by that script will inherit the ignored disposition. This is a distinct behavior compared to trapping a signal with an executable command, which Bash resets to the default disposition upon forking a child process.
  • Restoring Default Disposition: To stop ignoring a signal and revert it to its default operating system behavior, the trap command is invoked with a dash (-) as the command argument, or by omitting the command argument entirely.

# Revert SIGINT to its default disposition using a dash
trap - SIGINT


# Revert SIGINT to its default disposition by omitting the command argument
trap SIGINT
Master Bash with Deep Grasping Methodology!Learn More