I'm looking for a solution that will automatically allow me to call a function with specific parameters in a certain situation.
Here is the specific scenario:

When an error occurs, I can set up my error handler like this:

function _trap_err () {
    echo "An error occurred in "$1":"$2" on "$3":"$4".

And I can set the trap to collect the debugging data I need automatically – at the correct moment in execution.


AFAIK, this only works when bash detects an error and emits the signal.

I'd like to extend this behavior so that I can manually signal an error when a check fails, for example, when checking function arguments:

# Usage: myfunc <requiredarg>
function myfunc () {
    [[ $# != 1 ]] && emit_err_signal

If there is a way to manually emit that error signal, then its possible to automatically collect that debugging argument at the correct time in execution to provide meaningful debugging information.

The alternative is to simply display an error message – which has to be hardcoded in every check – and doesn't display where the error occurred.

Any ideas?

I should have specified that I this is for my .bashrc files and I don't want them to quit, I just want debugging information. Traps may have be overkill for this, i think the following solution may work:

function _debug () {
    local _message=${1:-""}
    printf "\e[1;32m[BASHRC] DEBUG: %-20s - %s\e[0m\n" "$BASH_SOURCE:$BASH_LINENO" "$_message"

_debug "Testing debug message."

> [BASHRC] DEBUG: .bashrc.d/bashrc:18  - Testing debug message.

You can use exit code :

set -e
# Usage: myfunc <requiredarg>
function myfunc () {
    [[ $# != 1 ]] && exit $ARGS_NUM_ERROR

Then you can trap ARGS_NUM_ERROR in your script with the suitable treatment.

  • set -e makes dangerous assumptions - specifically that all non-zero exit codes are failures. In this case it is useful, but probably moreso if placed within the body of the function than without it. – mikeserv Apr 27 '14 at 17:19
  • I should have specified in this in the question - in this case I'm trying to add some debugging information to my .bashrc file. I'm attempting to modularize it - putting specific chunks of configuration into .bashrc.d/conf.d & different utilities into .bashrc.d/lib.d - so I don't want the shell to exit if the error occurs, I'm just trying add some generic debugging information that can be toggle on/off with a flag. – nfarrar Apr 27 '14 at 17:31
  • I'm currently trying to figure out if the DEBUG trap can be used for this purpose - perhaps setting the DEBUG trap and then when I want to display a specific line, I would wrap a function call with something like _debug, which sets a flag, then the debug trap would echo the line and disable the flag. – nfarrar Apr 27 '14 at 17:34
  • 1
    @nfarrar - what of that other answer I wrote for you? ${unset?$(_function_call arg)} should work fine. – mikeserv Apr 27 '14 at 18:01

The ERR trap is triggered for any command with a nonzero exit status. There's no need for further explicit action to cause it to run.

function _trap_err () {
  echo "An error occurred in $1:$2 on $3:$4."

foo () {

echo "Some commands that succeed"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.