Skip to main content
added 243 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

In addition to LINENO containing the current line number, there are the BASH_LINENO and FUNCNAME (and BASH_SOURCE) arrays that contain the function names and line numbers they're called from.

So you could do something like this:

#!/bin/bash

error() {
        printf "'%s' failed with exit code %d in function '%s' at line %d.\n" "${1-something}" "$?" "${FUNCNAME[1]}" "${BASH_LINENO[0]}"
}

foo() {
        ( exit   0 ) || error "this thing"
        ( exit 123 ) || error "that thing"
}

foo

Running that would print

'that thing' failed with exit code 123 in function 'foo' at line 9.

If you do use set -e, or trap ... ERR to automatically detect errors, note that they have some caveats. It's also harder to include a description of what the script was doing at the time (as you did in your example), though that might be more useful to a regular user than just the line number.

See e.g. these for the issues with set -e and others:

In addition to LINENO containing the current line number, there are the BASH_LINENO and FUNCNAME (and BASH_SOURCE) arrays that contain the function names and line numbers they're called from.

So you could do something like this:

#!/bin/bash

error() {
        printf "'%s' failed with exit code %d in function '%s' at line %d.\n" "${1-something}" "$?" "${FUNCNAME[1]}" "${BASH_LINENO[0]}"
}

foo() {
        ( exit   0 ) || error "this thing"
        ( exit 123 ) || error "that thing"
}

foo

Running that would print

'that thing' failed with exit code 123 in function 'foo' at line 9.

If you do use set -e, or trap ... ERR to automatically detect errors note that they have some caveats. See:

In addition to LINENO containing the current line number, there are the BASH_LINENO and FUNCNAME (and BASH_SOURCE) arrays that contain the function names and line numbers they're called from.

So you could do something like this:

#!/bin/bash

error() {
        printf "'%s' failed with exit code %d in function '%s' at line %d.\n" "${1-something}" "$?" "${FUNCNAME[1]}" "${BASH_LINENO[0]}"
}

foo() {
        ( exit   0 ) || error "this thing"
        ( exit 123 ) || error "that thing"
}

foo

Running that would print

'that thing' failed with exit code 123 in function 'foo' at line 9.

If you use set -e, or trap ... ERR to automatically detect errors, note that they have some caveats. It's also harder to include a description of what the script was doing at the time (as you did in your example), though that might be more useful to a regular user than just the line number.

See e.g. these for the issues with set -e and others:

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

In addition to LINENO containing the current line number, there are the BASH_LINENO and FUNCNAME (and BASH_SOURCE) arrays that contain the function names and line numbers they're called from.

So you could do something like this:

#!/bin/bash

error() {
        printf "'%s' failed with exit code %d in function '%s' at line %d.\n" "${1-something}" "$?" "${FUNCNAME[1]}" "${BASH_LINENO[0]}"
}

foo() {
        ( exit   0 ) || error "this thing"
        ( exit 123 ) || error "that thing"
}

foo

Running that would print

'that thing' failed with exit code 123 in function 'foo' at line 9.

If you do use set -e, or trap ... ERR to automatically detect errors note that they have some caveats. See: