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 named command_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 of 127.
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).
scriptifcommandfails, use:command || script. – John1024 Jul 10 '16 at 17:56command_not_found_handletreats what aliases in a way that doesn't fit you? Give an example what you see it in your terminal. Write similar example explaining what you want to happen. Why do you writehandler, nothandle? (as a code, so it does matter). I'm down-voting your question for all these reasons. I will change my vote if you make the question clearer. – Kamil Maciorowski Jul 11 '16 at 04:24command_not_found_handlethat I at least came close to interpreting it correctly.) So far, *nobody* understands what you’re saying now. I have left places in your question highlighting the information that is missing. If you can fill those in, you might get some more help. Please do not respond in comments; [edit] your question to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Jul 11 '16 at 05:35