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?

+3


source to share


3 answers


bash has a callback command_not_found_handle

: http://www.gnu.org/software/bash/manual/bashref.html#Command-Search-and-Execution



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

+5


source


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
zounds!

      

+2


source


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,

http://www.linuxjournal.com/content/bash-command-not-found

+1


source







All Articles