help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] equivalent of set -e for function


From: Peng Yu
Subject: Re: [Help-bash] equivalent of set -e for function
Date: Tue, 4 Dec 2018 23:22:01 -0600

> :Just use some_command || return, though I guess you could do it with an ERR 
> trap. trap return ERR

I'd like to automatically check for errors. So some_command || return
is not an option as it will require the insertion of many trailing
returns.

I'd like to return from the function in trap ERR. But it seems that
the code trapped to ERR is considered to be outside of the function,
therefore can not be returned from the function. Is there any other
walkaround?

$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -E
trap 'declare -p BASH_COMMAND; { ( return ) } 2>/dev/null; echo "$?"' ERR
function f {
    false
    declare -p FUNCNAME
}
f

$ ./main.sh
declare -- BASH_COMMAND="false"
1
declare -a FUNCNAME=([0]="f" [1]="main")

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]