trap-event

Bash trap CHLD

Runs a handler for child completion events in Bash. Do not treat notification counts as a portable process accounting mechanism or replace explicit wait status collection. This fixture sets an idempotent marker, then collects the known child exit status. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): seen=1 status=4.

Formal Syntax & Structure

bash syntaxbash
trap 'handler' CHLD

Parameters & Attributes

NameTypeDescription
handlershell codeTrusted handler; keep it short.

Return Value

exit status

Registration status is separate from the child status returned by wait.

Working Example

Example Snippetbash
seen=0trap 'seen=1' CHLD( exit 4 ) &pid=$!wait "$pid"rc=$?trap - CHLDprintf 'seen=%s status=%s\n' "$seen" "$rc"
Bash Reference