Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the real line number of a failing Bash command?

Tags:

bash

In the process of coming up with a way to catch errors in my Bash scripts, I've been experimenting with "set -e", "set -E", and the "trap" command. In the process, I've discovered some strange behavior in how $LINENO is evaluated in the context of functions. First, here's a stripped down version of how I'm trying to log errors:

#!/bin/bash

set -E
trap 'echo Failed on line: $LINENO at command: $BASH_COMMAND && exit $?' ERR

Now, the behavior is different based on where the failure occurs. For example, if I follow the above with:

echo "Should fail at: $((LINENO + 1))"
false

I get the following output:

Should fail at: 6
Failed on line: 6 at command: false

Everything is as expected. Line 6 is the line containing the single command "false". But if I wrap up my failing command in a function and call it like this:

function failure {
    echo "Should fail at $((LINENO + 1))"
    false
}
failure

Then I get the following output:

Should fail at 7
Failed on line: 5 at command: false

As you can see, $BASH_COMMAND contains the correct failing command: "false", but $LINENO is reporting the first line of the "failure" function definition as the current command. That makes no sense to me. Is there a way to get the line number of the line referenced in $BASH_COMMAND?

It's possible this behavior is specific to older versions of Bash. I'm stuck on 3.2.51 for the time being. If the behavior has changed in later releases, it would still be nice to know if there's a workaround to get the value I want on 3.2.51.

EDIT: I'm afraid some people are confused because I broke up my example into chunks. Let me try to clarify what I have, what I'm getting, and what I want.

This is my script:

#!/bin/bash

set -E
function handle_error {
    local retval=$?
    local line=$1
    echo "Failed at $line: $BASH_COMMAND"
    exit $retval
}
trap 'handle_error $LINENO' ERR

function fail {
    echo "I expect the next line to be the failing line: $((LINENO + 1))"
    command_that_fails
}

fail

Now, what I expect is the following output:

I expect the next line to be the failing line: 14
Failed at 14: command_that_fails

Now, what I get is the following output:

I expect the next line to be the failing line: 14
Failed at 12: command_that_fails

BUT line 12 is not command_that_fails. Line 12 is function fail {, which is somewhat less helpful. I have also examined the ${BASH_LINENO[@]} array, and it does not have an entry for line 14.

like image 677
user108471 Avatar asked Jun 25 '14 01:06

user108471


People also ask

How do I show line numbers in bash?

In Bash, $LINENO contains the line number where the script currently executing. If you need to know the line number where the function was called, try $BASH_LINENO . Note that this variable is an array. See here for details of Bash variables.

Is 1 true or false in bash?

However, we can define the shell variable having value as 0 (“ False “) or 1 (“ True “) as per our needs.

How do I find bash errors?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command. $ echo $?


1 Answers

BASH_LINENO is an array. You can refer to different values in it: ${BASH_LINENO[1]}, ${BASH_LINENO[2]}, etc. to back up the stack. (Positions in this array line up with those in the BASH_SOURCE array, if you want to get fancy and actually print a stack trace).

Even better, though, you can just inject the correct line number in your trap:

failure() {
  local lineno=$1
  echo "Failed at $lineno"
}
trap 'failure ${LINENO}' ERR

You might also find my prior answer at https://stackoverflow.com/a/185900/14122 (with a more complete error-handling example) interesting.

like image 167
Charles Duffy Avatar answered Oct 16 '22 06:10

Charles Duffy