⚠️ Scripts WITHOUT Shebang Do NOT Work with command_not_found_handle ⚠️
The problem: without shebang handle does NOT work (fails to stop)
If you are relying on command_not_found_handle in your scripts. Make sure you are properly adding Shebang Hashbang to your scripts.
For example if you run the following example (without shebang):
main() {
echo "START-[\$\$=$$/$BASHPID]"
iDontExist
sleep 0.5
echo "FINISH-[\$\$=$$/$BASHPID]"
}
main "${@}"
You could be quite surprised to find out the following output, which shows that command_not_found_handle did trigger, BUT somehow the FINISH echo was allowed to execute:
START-[$$=2706051/2706051]
🤷 [$$=2706051/2706052] command_not_found_handle: invoked with arguments=['iDontExist'].
🤷 [$$=2706051/2706052] command_not_found_handle: Interrupting process group [2706051].
🤷 FunctionChain: [main:7 (scratch.sh)-->main:3 (scratch.sh)-->command_not_found_handle]
FINISH-[$$=2706051/2706051]
The fix: Add shebang
The fix is simple, add #!/usr/bin/env bash and it works as expected:
👍 Works as expected with shebang
#!/usr/bin/env bash
main() {
echo "START-[\$\$=$$/$BASHPID]"
iDontExist
sleep 0.5
echo "FINISH-[\$\$=$$/$BASHPID]"
}
main "${@}"
Output:
START-[$$=2707408/2707408]
🤷 [$$=2707408/2707409] command_not_found_handle: invoked with arguments=['iDontExist'].
🤷 [$$=2707408/2707409] command_not_found_handle: Interrupting process group [2707408].
🤷 FunctionChain: [main:10 (scratch.sh)-->main:5 (scratch.sh)-->command_not_found_handle]
FINISH echo is not reached.
Why it doesn't work.
At the time of writing I do not know why. Asking claude 4.5 sonnet gave some hallucination answers that were not correct.
Backlinks