Is this the appropriate place to present feature requests or is there
another forum for that?

I am keen to propose the introduction of an assert built-in.  The only
purpose of this built-in is to behave functionally identical to the eval
built-in, except that on error, the current shell process exits with an
appropriate exit code after outputting an assertion error and the line
number (possibly with the line of code that triggered the assert).

It's somewhat cumbersome to have to transport this assert function from
project to project, so it would save a considerable amount of effort and
time if it were built-in.  Here's an implementation I use, which achieves a
similar goal, but obviously doesn't capture the entire context as bash may
be able to internally. It is also a post execution assertion as opposed to
the one I proposed:

function assert() {
    local lineno=${BASH_LINENO[0]} \
          src=${BASH_SOURCE[1]} \
          lines=()

    mapfile -n 1 -t -s $(( $lineno - 1 )) lines < "$src"

    printf "ASSERT: ${src##*/}:$lineno: %s\n" "$lines"

    exit 1
}

Reply via email to