A function similar to Perl’s die() in bash

This post was written by eli on August 12, 2024
Posted Under: Linux

This is maybe a bit silly, but Perl has a die() function that is really handy for quitting a script with an error message. And I kind of miss it in Bash. So it can be defined with this simple one-liner:

function die { echo $1 ; exit 1 ; }

And then it can be used with something like:

unzip thefile.zip || die "Unzip returned with error status"

The Perl feeling, in Bash.

Add a Comment

required, use real name
required, will not be published
optional, your blog address