Session and utilities
The trap command
Schedules an action for when the script exits or receives a signal.
Syntax
trap 'accion' SENALCopy-ready examples
$ printf 'trap "echo adios" EXIT\necho fin\n' > t.shScript with trap$ bash t.shOn exit, prints adiosYou can try them in the browser terminal: it does not touch your computer.
Typical mistakes
- EXIT always runs at the end; useful to clean up temporary files.
FAQ
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course