I'm trying to create some error reporting using a Trap to call a function on all errors:Trap _func ERRIs it possible to get what line the ERR signal was sent from? The shell is bash.If I do that, I can read and report what command was used and log/perform some actions.Or maybe I'm going at this all wrong?I tested with the following:#!/bin/bashtrap ECHO $LINENO ERRecho hello | grep asdfAnd $LINENO is returning 2. Not working.
Trap, ERR, and echoing the error line
bash;shell script;error handling;trap
As pointed out in comments, your quoting is wrong. You need single quotes to prevent $LINENO from being expanded when the trap line is first parsed.This works:#! /bin/basherr_report() {    echo Error on line $1}trap 'err_report $LINENO' ERRecho hello | grep foo  # This is line number 9Running it: $ ./test.sh Error on line 9