Is there a way to grab a custom bash command that was not found and do something with it?

Suppose I type saklfjsklj in bash, I get:

bash: saklfjsklj: command not found


Is there a way to intercept this and execute the script?


source to share

3 answers

bash has a callback command_not_found_handle


There is information here about this answer on about messy default usage.



Define a function named command_not_found_handle

. The command and arguments will be passed as arguments to the function.

$ command_not_found_handle() { echo "$@"'!'; }
$ zounds




From the bash man page,

* If the search is unsuccessful, the shell looks for a specific shell named command_not_found_handle. If this function exists, it is called with the original command and the original command arguments as its arguments, and the exit status of the function becomes the shell exit status. If this function is not defined, the shell prints an error message and returns an exit status of 127. *

I also found this article on this subject,



All Articles