caller 0 gives wrong line number in trap handler

When using the caller builtin in a Bash trap function, the result of caller 0 gives the wrong line number, always giving 1 . For example:

#!/bin/bash
function foo {
    exit 1
}
function bar {
    foo
}
function err {
    (( i = 0 ))
    while caller $i; do
        (( ++i ))
    done
}
trap err EXIT
bar

gives the following output:

1 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh

While the output for i > 0 is correct, when using caller 0 in a trap handler it always seems to give 1 as the line number. Is there any way to get the real line number of the failed function from a trap handler?


This appears to be a bug introduced sometime after 3.2.57(1)-release:

$ bash -version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.

$ bash ./test.sh
3 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh

$ /usr/local/bin/bash --version
GNU bash, version 4.3.30(1)-release (x86_64-apple-darwin14.1.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ /usr/local/bin/bash ./test.sh
1 foo ./test.sh
6 bar ./test.sh
15 main ./test.sh

There seems to already be a bug report with the bash project.

链接地址: http://www.djcxy.com/p/23756.html

上一篇: BroadcastReceiver onReceive()不动态注册时调用

下一篇: 调用者0在陷阱处理程序中给出错误的行号