From the course: Linux: Bash Shell and Scripts

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Signals and traps

Signals and traps - Linux Tutorial

From the course: Linux: Bash Shell and Scripts

Start my 1-month free trial

Signals and traps

- [Instructor] Bash has a command, trap, that you use for catching signals and handling signals. You can even just ignore the signal, if you want. A handy thing to use the trap for is to gracefully die when the user does Control + C, instead of leaving behind some temporary files, for example, you can clean those up, and then terminate. The way the trap command works is you give the trap command a string of stuff to do, and then the name of the signal that you wanted to do that stuff for. Control + C is the interrupt signal, and the name of that is INT. Control + Backslash is the quit signal. So here we have a little script. We changed directory to slash, and we go into a seemingly infinite loop, and we print out looping, and then we do a du command, disk usage, and megabytes for everything. That'll take a little while, and that's gonna be looping and printing out the amount of disk space that all of the directories are taking. And we're gonna send error messages to dev/null, in case…

Contents