My goal: when I type in something that's not a valid Unix command, it gets fed to a special function instead of displaying the "Command not found" message.
I found this post, which got me halfway there.
trap 'if ! type -t $BASH_COMMAND >/dev/null; then special_function $BASH_COMMAND; fi' DEBUG
This allows me to run my special function. However the "Command not found" error still appears afterwards.
Is there any way I can tell Bash to suppress that message? Either in this command, or inside the special function, would be fine. Thanks!
The only thing I can think of is redirecting stderr. I don't think it's a great solution but could be a starting point. Something like:
Seems a bit clumsy but could work with some adaptation.
In ZSH; this can be achieved by adding this to the .zshrc
This will behave in a very weird way in bash; because bash sends the prompt to stderr ( In effect you will be able to see the prompt and anything you type only after pressing the Enter key ). ZSH on the other hand, handles the prompt and stderr as separate streams.
If you can find a way to make bash to send the prompt to some other location ( say /dev/tty ) something similar will work in bash also.
EDIT :
It seems that bash versions > 4 have a
command_notfound_handle
function that can do what you want. You can define it in your ~/.bashrc