Session and utilities

The trap command

Schedules an action for when the script exits or receives a signal.

Syntax

trap 'accion' SENAL

Copy-ready examples

$ printf 'trap "echo adios" EXIT\necho fin\n' > t.shScript with trap
$ bash t.shOn exit, prints adios

You 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.

Related commands

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