What is the name of the command (function) that runs after a command has failed?
I have this fragment in my /etc/bash.bashrc
(Ubuntu 14.04.4 LTS):
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi
It looks like you should overwrite the command_not_found_handle
function. The package command-not-found
is not required for this to work. Indeed, this is what Bash Reference Manual says:
If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of
$PATH
for a directory containing an executable file by that name. […] If the search is unsuccessful, the shell searches for a defined shell function namedcommand_not_found_handle
. If that function exists, it is invoked in a separate execution environment with the original command and the original command’s arguments as its arguments, and the function’s exit status becomes the exit status of that subshell. If that function is not defined, the shell prints an error message and returns an exit status of127
.
Example:
function command_not_found_handle { echo BOOM! ; }
Result:
$ foo12345
BOOM!
$ echo "echo is valid command"
echo is valid command
$ agrgokdnlkdgnoajgldfnsdalf grhofhadljh
BOOM!
$ cat /etc/issue
Ubuntu 14.04.4 LTS \n \l
$ catt /etc/issue
BOOM!
To revert (quick and dirty):
# Assuming you haven't modified /etc/bash.bashrc
. /etc/bash.bashrc
# Quick and dirty, because if your ~/.bashrc or ~/.bash_profile //
# overwrites some settings from /etc/bash.bashrc //
# you need to source them again.
# Things may get complicated, I won't cover all the ifs here.
# Logout and login again for the clean start.
Modify /etc/bash.bashrc
to change "command not found" behavior for all users. Define your own command_not_found_handle
in ~/.bashrc
to make it work for you only. Or write two files with proper function definitions to enable and disable your hack anytime. Important: do not execute the files, source them like this:
. ~/.hack_enable
. ~/.hack_disable
Where .hack_enable
defines your function, .hack_disable
goes back to the original one (from the first codeblock of my answer or to something similar what is right in your case).