I wrote a function to handle unknown commands in bash via command_not_found_handle (). My intention is to source scripts on demand and access its functions inside the handler. But unfortunately the sourced functions run in a subshell so variables are lost after return/exit.

Finally I did it with some signal/trap/fifo mechanism to command_not_found_handle's parent shell to achieve my goal, but its everything but elegant.

Now I am wondering if function_not_found_handle loadable builtin could be implemented. The builtin should be called if command_not_found_handle () fails.

For the time being I found a great article by Ed Schaefer about this subject, but it seems to be an entrance point.

Is anyone experienced with dynamically loadable builtin bash commands? Who could point me to useful information?

I hope to come up with some results soon to concretize my question.

Thank you!

  • 2
    I don't think there's any way for the shell to know if the command you're looking for should be a function or an external command. It would have to know beforehand to decide which x_not_found() to call. – ilkkachu Feb 3 '18 at 20:21
  • The priority of x_not_found () functions could also follow the shell command search sequence as described here, meaning: ! function_not_found_handle $@ || command_not_found_handle $@ On the other hand, found commands started by command_not_found_handle are started as separate process anyway, so in fact its current behavior is questionable to me. – Dominik Kummer Feb 5 '18 at 8:28

Your Answer

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

Browse other questions tagged or ask your own question.