When run a command, if the command is not present, then some information regarding the failure of the command is displayed.

I am trying to take the information about that failed command as input to my script, which has to run automatically whenever the command fails.

Whenever the command fails the $? value would be 127. I have to catch this failed event and run my commands there.

  • 2
    To run script if command fails, use: command || script.
    – John1024
    Jul 10, 2016 at 17:56
  • @DavidPostill Hey You miss understanding me .Here I am not asking for the script to write the developers. I am asking for what script file should i edit to catch the failed command events
    – Ankanna
    Jul 10, 2016 at 18:00
  • 1
    Possible duplicate of Hook into "command not found" handler in Ubuntu Jul 10, 2016 at 20:28
  • 1
    (1) This is not a correspondence.  If we’ve helped you make some progress in solving your problem, do not blow away your question and rewrite it as a follow-up.  To a limited extent, you may add to your question, refining it (rather than replacing it).  I’ve made a first cut at doing that for you.  … (Cont’d) Jul 11, 2016 at 5:35
  • 2
    (Cont’d) …  (2) Unfortunately, you have not come close to explaining it clearly.  Apparently nobody before me understood the first version of your question.  (I’m presuming from the fact that you are trying to work with command_not_found_handle that 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. Jul 11, 2016 at 5:35

2 Answers 2


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 $?
           printf "%s: command not found\n" "$1" >&2
           return 127

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.


function command_not_found_handle { echo BOOM! ; }


$ foo12345
$ echo "echo is valid command"
echo is valid command
$ agrgokdnlkdgnoajgldfnsdalf grhofhadljh
$ cat /etc/issue
Ubuntu 14.04.4 LTS \n \l

$ catt /etc/issue

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).

  • Thanks a lot for the good explanatory answer ,If I do like this it is working but my aliases are also treating as command not found things.how can treat them different?
    – Ankanna
    Jul 11, 2016 at 2:17
  • Except for aliases it works great . Can you help me in how to skip aliases ?I have edited the question as the question seems to be bit confusing earlier . Thanks for the quick answer
    – Ankanna
    Jul 11, 2016 at 2:56

Try the following script:

if command ; then
    echo "Command succeeded"
    echo "Command failed"

Like this you can execute whatever code you want in each case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .