Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When bash encounters an unknown command(word?) it does this :

The program 'hello' can be found in the following packages:
 * hello
 * hello-debhelper
Try: sudo apt-get install <selected package>

what I would like to know is how this is done so I can edit it or add something before it to cross check the unknown word from a home grown dictionary which would have phrase:reply pairs that can then be sent to output.

I am guilty of not looking enough for it around .. but the few bash guides I tried digging up didn't have anything on this. Maybe I am looking at the wrong places .. any pointers?

And yes I am doing this so every time I type wtf when a program fails , I want something nice thrown back at me...

share|improve this question
    
While we're at it, how do you disable this altogether? –  user606723 Oct 28 '11 at 17:31
2  
@user606723 see askubuntu.com/q/72853/6969 –  Lekensteyn Oct 28 '11 at 18:01
    
Another useful link: wiki.ubuntu.com/CommandNotFoundMagic –  glenn jackman Oct 28 '11 at 18:08

5 Answers 5

up vote 16 down vote accepted

Look in your /etc/bash.bashrc for the command_not_found_handle function definition.

If you want to remove that behaviour, put this in your .bashrc

[[ $(type -t command_not_found_handle) = "function" ]] && 
  unset -f command_not_found_handle

If you want to customize, you can do

# see http://stackoverflow.com/questions/1203583/how-do-i-rename-a-bash-function
alias_function() {
  eval "${1}() $(declare -f ${2} | sed 1d)"
}

alias_function orig_command_not_found_handle command_not_found_handle 

command_not_found_handle() {
  command=$1
  shift
  args=( "$@" )

  do your stuff before
  orig_command_not_found_handle "$command" "${args[@]}"
  do your stuff after
}
share|improve this answer
1  
I like this approach. –  ændrük Oct 28 '11 at 18:00
    
wow! I liked the alias_function idea :-) –  anishsane May 28 '13 at 5:00

This might be potentially useful...

The command-not-found package is what gives you the magic response. I'm not sure if it's possible to customize it, but it might be worth a look.

Another option to do what I think what you're trying to do would be to add an alias to your .bashrc file that prints a message whenever you type 'wtf' or something like that:

alias wtf='echo "chill out man"'

Add this to your ~/.bashrc file, and then do: source $HOME/.bashrc

This would then just print a message whenever you type wtf into your terminal. You could also make this alias call a script that prints a more detailed message or something similar. The possibilities are endless!

share|improve this answer

This behavior is defined in the system-wide Bash configuration file, /etc/bash.bashrc:

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/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/bin/python /usr/lib/command-not-found -- "$1"
                   return $?
                elif [ -x /usr/share/command-not-found ]; then
           /usr/bin/python /usr/share/command-not-found -- "$1"
                   return $?
        else
           return 127
        fi
    }
fi

To customize it, simply override this function in your own ~/.bashrc:

function command_not_found_handle {
  echo "Sorry, smotchkiss, try again."
}
share|improve this answer

@user606723, if you want to get rid of this behavior in it's entirety:

sudo apt-get remove command-not-found command-not-found-data 

If that doesn't work, try this:

sudo apt-get purge command-not-found command-not-found-data 

If you want to get the behavior back:

sudo apt-get install command-not-found
share|improve this answer

As an addition to the comment from glenn jackman.

Use fortune with an input file in the form:

A quote.
%
Another quote.
%
share|improve this answer
    
I fail to understand how a custom fortune quote would be triggered by a command not found. –  MestreLion Nov 18 '11 at 2:00
    
Thanks for pointing this out. I meant in addition to the top comment. –  Dimitris Leventeas Nov 18 '11 at 13:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.