mainThe GNU Bourne-Again SHell - Support: sr #108749, RETURN trap does not see the exit...

 
 

sr #108749: RETURN trap does not see the exit status of 'return'

Submitter:  Paul Donohue <paulsd>
Submitted:  Thu 19 Feb 2015 06:39:29 PM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Done
Privacy:  Public Assigned to:  None
Open/Closed:  Open Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 13 Nov 2015 05:22:30 PM UTC, comment #3: 

I don't know who is allowed to close bugs.  I don't have the appropriate privilege to do it.  The best I can do is mark it `done'.

In any event, the RETURN trap is run in the function's context, before the caller regains control.  In that sense, it's part of the return command, and in that context, the value of $? is the exit status of the last command executed before the `return'.

Chet Ramey <chet>
Group administrator
Tue 23 Jun 2015 09:00:34 PM UTC, comment #2: 

Thanks for the work-around.

Some additional info:
http://lists.gnu.org/archive/html/bug-bash/2015-02/msg00113.html

I would close this bug, but it doesn't look like I am allowed to do so.

Paul Donohue <paulsd>
Wed 25 Mar 2015 09:32:23 AM UTC, comment #1: 

As a workaround, you can do:

f() {
local ret
trap 'inspect $ret here' RETURN
ret=2
return "$ret"
}

Eduardo Bustamante <dualbus>
Thu 19 Feb 2015 06:39:29 PM UTC, original submission:  

Example:
$ test_fun()
{
  trap 'echo returned $?' RETURN
  false  # exit status is 1
  return 2
}
$ test_fun
returned 1
$

I intuitively expected the above to print 2 instead of 1.

The bash man page states "Any command associated with the RETURN trap is executed before execution resumes after the function or script."  This is a bit vague, but it seems to imply that the RETURN trap should run after the `return` command is complete (since the `return` command is part of the function and the RETURN trap runs "after the function"), which would imply that $? should be set to the exit status of the return command in the RETURN trap.  So, the documentation seems to back up my intuition here...

The problem I was actually trying to solve was to write a trap that ran only if the function returned an error.  An ERR trap would have been run if any command within the function returned an error, which was not what I wanted.  So, I simply wrote a RETURN trap which checked $?, but $? did not give me the return status of the function.

Is there a reason for the current behavior?  Could it be changed such that $? in a RETURN trap gives the return status of the function?

Paul Donohue <paulsd>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by dualbus (Posted a comment)
  • -email is unavailable- added by paulsd (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-11-13 chet StatusNone Done

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code