What causes bash to pause after a bad command

bash

When you are typing a bad command, say you want to do:

$ cd ..
$ ls

But instead you type too fast and end up hitting enter between l and s, so you get:

$ cd ..
$ l
sbash: l: command not found...

This happens all the time of course, when we type fast. What I'm curious about is the source of the 2.5 second (on my system) pause after the bad command text appears on the screen. Seems to me it recognizes the bad command instantly, prints error message about it, but then just for fun waits 2.5 seconds before returning me to the command line. What causes this? Is there a way fix to this? It's a small annoyance, but I'd like to get rid of it. 🙂

Edit: Running Fedora 16 system

Best Answer

This type of behavior is caused by a tool frequently installed on some distributions that hook into bash. This hook makes it so that if you try to run a command, and the command doesn't exist, then bash will search the files available in the configured repositories, and tell you what package you would need to install to get that command.

If you are not on a Debian system you'll want to look at your bash startup files profiles and so on and see if anything defines the function named command_not_found_handle. If that bash function is defined, then it will be called whenever you run a command and an appropriate program cannot be found in your search path. If you run typset | less and browse through the output you will see the command_not_found_handle() function if it has been defined.

On Debian/Ubuntu the package that provides this behavior is command-not-found. If you purge that, then you will disable the lookups which does slow things down.

Here is an example

# command-not-found installed
$ time pwgen
The program 'pwgen' is currently not installed.  To run 'pwgen' please ask your administrator to install the package 'pwgen'
pwgen: command not found

real    0m0.074s
user    0m0.032s
sys     0m0.040s

# purge command-not-found and restart bash
$ time pwgen
-bash: pwgen: command not found

real    0m0.002s
user    0m0.000s
sys     0m0.000s

The exact time would of course be different for you. I ran my tests on a pretty beefy server.

Related Question