Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using VMS DCL command scripts one can catch errors by using the on error command:

$ on error then goto MyErrorHandler
$ directory samiam.txt
$ delete x.txt
$ exit
$ print "Something."

How does one do this in Linuxland?

In the example an error with directory or delete will cause control to go to MyErrorHandler.

share|improve this question

migrated from stackoverflow.com Feb 12 '11 at 20:20

This question came from our site for professional and enthusiast programmers.

VMS, my old friend! – Luc M Feb 15 '11 at 3:34
up vote 8 down vote accepted

I think you want the trap function, specifically:

    echo 'An error occurred!'
    exit 1

trap error_func ERR

Errors later will jump to the function. This is supported by at least bash, zsh, and ksh.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.