In bash, is there an equivalent of die "error msg"

In perl, you can exit with an error msg with die "some msg". Is there an equivalent single command in bash? Right now, I'm achieving this using commands: echo "some msg" && exit 1


Solution 1:

You can roll your own easily enough:

die() { echo "$*" 1>&2 ; exit 1; }
...
die "Kaboom"

Solution 2:

Here's what I'm using. It's too small to put in a library so I must have typed it hundreds of times ...

warn () {
    echo "$0:" "$@" >&2
}
die () {
    rc=$1
    shift
    warn "$@"
    exit $rc
}

Usage: die 127 "Syntax error"