simple Exit in Handle Will Not Work

The catch is that command_not_found_handle will run in separate execution.

So even when we add the command_not_found_handle with an exit

#!/usr/bin/env bash

echo_with_pid(){
  echo "[\$\$=$$/$BASHPID] $*"
}

command_not_found_handle() {
  echo_with_pid "command_not_found_handle invoked with arguments=[$*]"
  exit 127
}

main() {
  echo_with_pid "START"
  i_dont_exist hi1 hi2
  echo_with_pid "DONE"
}

main "${@}"

Output:

[$$=21132/21132] START
[$$=21132/21133] command_not_found_handle invoked with arguments=[i_dont_exist hi1 hi2]
[$$=21132/21132] DONE

We still end up running over the command not found issue. Albeit at least now we have a piece of code that executes on this event.

We can see by $BASHPID value 21133 that command_not_found_handle ran in a separate process, then our main function. So when we call exit in command_not_found_handle we are exiting process 21133 NOT 21132, which is running our main function.


Backlinks