2

When running invalid commands without any parameters or options, in my experience we get only two kinds of messages which are exemplified below:

~$ Date
No command 'Date' found, did you mean:
 Command 'yate' from package 'yate' (universe)
 Command 'date' from package 'coreutils' (main)
 Command 'late' from package 'late' (universe)
 Command 'kate' from package 'kate' (universe)
Date: command not found
~$ fjldjflsk
fjldjflsk: command not found
~$

I'm interested in knowing more about how this works. Specifically, when can I know which of these two kinds of messages I'm going to get prior to running the command? Is there some kind of environment variable or any other device that would allow me to alter this behavior easily? I'd like to get only the second kind of message.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936

1 Answers1

3

The bash shell calls a function called command_not_found_handle whenever a command is not found.

This function can be viewed with declare -f command_not_found_handle and may look something like this (found on an Ubuntu system):

command_not_found_handle ()
{
    if [ -x /usr/lib/command-not-found ]; then
        /usr/lib/command-not-found -- "$1";
        return $?;
    else
        if [ -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" 1>&2;
            return 127;
        fi;
    fi
}

As you can see, it use another tools called command-not-found. It also uses unquoted variable expansions and unnecessary return statements, but that's beside the point at the moment.

To remove the predictions that this function makes, you can either remove this function altogether using

unset -f command_not_found_handle

(this is probably the best option) or, you can uninstall the executable that the function uses, assuming a system using apt:

sudo apt purge command-not-found

(the command-not-found command is part of the package of the same name on e.g. Ubuntu at least).

For more information about the command_not_found_handle function, see the bash manual.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • Hmm, I just did the unset command and ran the same command only with set instead of unset, but didn't retrieve the functionality. Do I see to restart my shell? (I'd rather not at the moment). – command_not_found Feb 17 '19 at 22:25
  • @command_not_found You could try `source /etc/bash.bashrc` (or whatever the equivalent file is called on your system). On the Ubuntu machine I have access to, that file sets up the `command_not_found_handle` function. – Kusalananda Feb 17 '19 at 22:32
  • It does not have to be a Linux, to run apt (E.g. Ubuntu on WSL). – ctrl-alt-delor Feb 17 '19 at 23:21
  • @ctrl-alt-delor Never said so either. – Kusalananda Feb 17 '19 at 23:24
  • “…you can uninstall the executable that the function uses, assuming a Linux using apt”: It does not have to be a Linux, to run apt (E.g. Ubuntu on WSL). – ctrl-alt-delor Feb 17 '19 at 23:26
  • @ctrl-alt-delor Sure I'll change it (you could have done too). – Kusalananda Feb 17 '19 at 23:27