Note: this is not a duplicate. In bash, is there an equivalent to die "error msg" , as shown at the end of this post.
Consider the shell function
foo () { echo "testing..." 1>&2 return 1 echo "should never reach this point" 1>&2 }
The following is the expected behavior of foo :
% foo || echo $? testing... 1
I would like to encapsulate the functionality shown in the first two lines of foo in the die function, so that the definition of foo can be reduced to
foo () { die 1 "testing..." echo "should never reach this point" 1>&2 }
... while maintaining its original behavior.
My interest is primarily zsh , but it will also be interesting for answers suitable for bash scripts and / or /bin/sh if they are different.
By the way, this will not work:
die () { local exit_code=$1 shift echo "$*" 1>&2 exit $exit_code }
If the version of foo that used this die was selected from the command line, the result would be to kill one current shell (at least this is the result I get when I try something like this). This is why this question is not a duplicate. In bash, is there an equivalent to die "error msg" . Anyway, the answer to this other question will not satisfy my requirements here.
source share