mainThe GNU Bourne-Again SHell - Support: sr #108750, $LINENO and $BASH_SOURCE are...

 
 

sr #108750: $LINENO and $BASH_SOURCE are inconsistent in ERR trap on function return

Submitter:  Paul Donohue <paulsd>
Submitted:  Thu 19 Feb 2015 07:32: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
   

Tue 23 Jun 2015 09:05:02 PM UTC, comment #1: 

This has been fixed:
http://lists.gnu.org/archive/html/bug-bash/2015-02/msg00132.html

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

Paul Donohue <paulsd>
Thu 19 Feb 2015 07:32:29 PM UTC, original submission:  

When an ERR trap is called, ${BASH_SOURCE[0]}, ${FUNCNAME[0]}, and $LINENO normally indicate the file/function/line which called the command that returned a non-zero status.  This is good.

However, if an ERR trap is called because a function returned a non-zero status, then ${BASH_SOURCE[0]} and ${FUNCNAME[0]} still indicate the file/function which called the function that returned a non-zero status, but $LINENO indicates the line number of the last command in the function that was called, and the line number of the call to the function is not available.

Example:
$ cat <<'END' >script1
test_fun() {
  return 1
}
END
$ cat <<'END' >script2
#!/bin/bash
# Some blank lines to adjust the line numbers relative to script1

source script1
trap 'echo error at: ${BASH_SOURCE[0]} ${FUNCNAME[0]} $LINENO ${BASH_LINENO[@]}' ERR
false  # Trigger the ERR trap with a command on line 6
test_fun  # Trigger the ERR trap with a function on line 7
END
$ chmod 755 script2
$ ./script2
error at: ./script2 main 6 0
error at: ./script2 main 2 0
$

This seems like a bug.  I would expect one of two behaviors here:
1) In the ERR trap on function return, $LINENO should point to the line which called the function (just as ${BASH_SOURCE[0]} and ${FUNCNAME[0]} point to the file/function which called the function that returned a non-zero status).
2) In the ERR trap on function return, ${BASH_SOURCE[0]}, ${FUNCNAME[0]}, and $LINENO should point to the last command executed within the function, and ${BASH_SOURCE[1]} ${FUNCNAME[1]} ${BASH_LINENO[0]} should point to the caller of the function.

Thoughts?

Thanks!

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 (Updated the item)
  • -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-f8d8.
    Corresponding source code