11

I want to hook into the handler for command not found

wim@SDFA100461C:~$ thing
No command 'thing' found, did you mean:
 Command 'tping' from package 'lam-runtime' (universe)
 Command 'thin' from package 'thin' (universe)
thing: command not found

I want to override this behaviour with my own script.

Specifically, I want to check if the command exists in the output of lsvirtualenv -b, and if so I want to activate that virtualenv.

Where should I start hacking?

1

2 Answers 2

2

For bash, its behavior is governed by the shell function command_not_found_handle (See man bash, under COMMAND EXECUTION).

To see what behavior is defined by that function, you can issue:

declare -p -f command_not_found_handle

You can change which program is used by redefining the command_not_found_handle function.

In Ubuntu 14.04 LTS, it seems the default behavior is defined directly in /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/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 $?
            else
               printf "%s: command not found\n" "$1" >&2
               return 127
            fi
    }
fi
10

In General

The Linux Journal has a pretty good article:

From bash's man page:

... A full search of the directories in PATH is performed only if the command is not found in the hash table. 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 with the original command and the original command's arguments as its arguments, and the function's exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127.

and

A quick grep in /etc discovered where it was happening. The function itself is in /etc/bash_command_not_found and that function gets included (if it exists) in your bash session via /etc/bash.bashrc.

Ubuntu 14.04

Empirical evidence suggests that on an Ubuntu 14.04 installation, the file /etc/bash_command_not_found doesn't exist, however, the correct file is a python script, located at /usr/lib/command-not-found

2
  • 2
    This set me on the right path, but the actual program was a python script located at /usr/lib/command-not-found. On my Ubuntu 14.04 installation, the file /etc/bash_command_not_found doesn't exist.
    – wim
    Commented Jul 25, 2014 at 13:36
  • Thanks, I've added that into my answer for future viewers Commented Jul 28, 2014 at 7:36

You must log in to answer this question.

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