21

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

21

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
}
  • 1
    I like this approach. – ændrük Oct 28 '11 at 18:00
  • 1
    wow! I liked the alias_function idea :-) – anishsane May 28 '13 at 5:00
  • To see/check the current definition, do: declare -p -f command_not_found_handle – Randall Sep 22 '17 at 20:06
4

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!

3

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."
}
0

@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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