{
	"id": "37c68b52-3242-4e85-8963-32f28d4bac09",
	"created_at": "2026-04-06T00:09:33.588632Z",
	"updated_at": "2026-04-10T13:12:05.973347Z",
	"deleted_at": null,
	"sha1_hash": "9611364b2c89a568f5aecd83fa399b9031321b4b",
	"title": "Trap statement - Linux Bash Shell Scripting Tutorial Wiki",
	"llm_title": "",
	"authors": "",
	"file_creation_date": "0001-01-01T00:00:00Z",
	"file_modification_date": "0001-01-01T00:00:00Z",
	"file_size": 49473,
	"plain_text": "Trap statement - Linux Bash Shell Scripting Tutorial Wiki\r\nArchived: 2026-04-05 12:53:25 UTC\r\n← Shell signal values • Home • How to clear trap →\r\nWhile running a script user may press Break or CTRL+C to terminate the process.\r\nUser can also stop the process by pressing CTRL+Z.\r\nError can occur do to bug in a shell script such as arithmetic overflow.\r\nThis may result into errors or unpredictable output.\r\nWhenever user interrupts a signal is send to the command or the script.\r\nSignals force the script to exit.\r\nHowever, the trap command captures an interrupt.\r\nThe trap command provides the script to captures an interrupt (signal) and then clean it up within the script.\r\nSyntax\r\nThe syntax is as follows\r\ntrap arg signal\r\ntrap command signal\r\ntrap 'action' signal1 signal2 signalN\r\ntrap 'action' SIGINT\r\ntrap 'action' SIGTERM SIGINT SIGFPE SIGSTP\r\ntrap 'action' 15 2 8 20\r\nExample\r\nCreate a shell script called testtrap.sh:\r\n#!/bin/bash\r\n# capture an interrupt # 0\r\ntrap 'echo \"Exit 0 signal detected...\"' 0\r\n# display something\r\necho \"This is a test\"\r\n# exit shell script with 0 signal\r\nexit 0\r\nSave and close the file. Run it as follows:\r\nhttps://bash.cyberciti.biz/guide/Trap_statement\r\nPage 1 of 3\n\nchmod +x testtrap.sh\r\n./testtrap.sh\r\nSample outputs:\r\nThis is a test\r\nExit 0 signal detected...\r\nThe first line sets a trap when script tries to exit with status 0.\r\nThen script exits the shell with 0, which would result in running echo command.\r\nTry the following example at a shell prompt (make sure /tmp/rap54ibs2sap.txt doesn't exits).\r\nDefine a shell variable called $file:\r\nfile=/tmp/rap54ibs2sap.txt\r\nNow, try to remove $file, enter:\r\nSample output:\r\nrm: cannot remove `/tmp/rap54ibs2sap.txt': No such file or directory\r\nNow sets a trap for rm command:\r\ntrap \"rm $file; exit\" 0 1 2 3 15\r\nDisplay list of defined traps, enter:\r\nSample outputs:\r\ntrap -- 'rm /tmp/rap54ibs2sap.txt; exit' EXIT\r\ntrap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGHUP\r\ntrap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGINT\r\ntrap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGQUIT\r\ntrap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGTERM\r\nNow, try again to remove the $file, enter:\r\nThis time rm command did not displayed an error. The $file doesn't exist yet. The trap command simply exit\r\nwhenever it get 0, 1, 2, 3, or 15 signal. Try capturing CTRL+C:\r\n#!/bin/bash\r\n# capture an interrupt # 2 (SIGINT)\r\nhttps://bash.cyberciti.biz/guide/Trap_statement\r\nPage 2 of 3\n\ntrap '' 2\r\n# read CTRL+C from keyboard with 30 second timeout\r\nread -t 30 -p \"I'm sleeping hit CTRL+C to exit...\"\r\nSample outputs:\r\nI'm sleeping hit CTRL+C to exit...^C^C^C^C\r\n← Shell signal values • Home • How to clear trap →\r\nSource: https://bash.cyberciti.biz/guide/Trap_statement\r\nhttps://bash.cyberciti.biz/guide/Trap_statement\r\nPage 3 of 3",
	"extraction_quality": 1,
	"language": "EN",
	"sources": [
		"MITRE"
	],
	"origins": [
		"web"
	],
	"references": [
		"https://bash.cyberciti.biz/guide/Trap_statement"
	],
	"report_names": [
		"Trap_statement"
	],
	"threat_actors": [],
	"ts_created_at": 1775434173,
	"ts_updated_at": 1775826725,
	"ts_creation_date": 0,
	"ts_modification_date": 0,
	"files": {
		"pdf": "https://archive.orkl.eu/9611364b2c89a568f5aecd83fa399b9031321b4b.pdf",
		"text": "https://archive.orkl.eu/9611364b2c89a568f5aecd83fa399b9031321b4b.txt",
		"img": "https://archive.orkl.eu/9611364b2c89a568f5aecd83fa399b9031321b4b.jpg"
	}
}