How Ubuntu's command suggestion feature works?

Have you ever wondered how Ubuntu gives you suggestions when you enter a wrong command name on a terminal? This feature is a great way to quickly figure out the error and even helps to find out the package that provide a particular tool. This task is taken care of by the command_not_found_handle() function in /etc/bash.bashrc.

This is how command_not_found_handle is defined on Ubuntu 10.10, Maverick Meerkat.

[shredder12]$ type command_not_found_handle

command_not_found_handle is a function
command_not_found_handle ()
{
    if [ -x /usr/lib/command-not-found ]; then
        /usr/bin/python /usr/lib/command-not-found -- $1;
        return $?;
    else
        if [ -x /usr/share/command-not-found ]; then
            /usr/bin/python /usr/share/command-not-found -- $1;
            return $?;
        else
            return 127;
        fi;
    fi
}

If you notice the output above, command_not_found_handle looks for /usr/lib/command-not-found and executes it as a python script otherwise returns error 127.

If you are more curious, open the /usr/lib/command-not-found file and take a look at the script. It works using the python module CommandNotFound.

So, now you know how Ubuntu takes care of your terminal errors .

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.