# How does CommandNotFound Work?

**URL:** <https://community.unix.com/t/how-does-commandnotfound-work/296531>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [October 4, 2011, 12:50am UTC](https://community.unix.com/t/how-does-commandnotfound-work/296531 "2011-10-04T00:50:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vistastar](https://community.unix.com/user_avatar/community.unix.com/vistastar/32/2003_2.png) [@vistastar](https://community.unix.com/u/vistastar)\
**Post date:** [October 4, 2011, 12:50am UTC](https://community.unix.com/t/how-does-commandnotfound-work/296531/1 "2011-10-04T00:50:28Z")

</div>

Usually, when we typed a non-exist command, an error message"command not found" occurs. But, some times shell is smart than this by giving more infomation:

```nohighlight
[alien@ubuntu~/tmp]:)zsh
The program 'zsh' can be found in the following packages:
 * zsh-beta
 * zsh
Ask your administrator to install one of them
-bash: zsh: command not found
[alien@ubuntu~/tmp]:(john
The program 'john' is currently not installed. To run 'john' please ask your administrator to install the package 'john'
-bash: john: command not found
[alien@ubuntu~/tmp]:(which zsh
[alien@ubuntu~/tmp]:(which john

```

I find the code

```nohighlight
print >>sys.stderr, _("The program '%s' is currently not installed. ") % command

```

in /usr/share/pyshared/CommandNotFound/CommandNotFound.py

I am wondering how bash make use of the CommandNotFound.py to print the smart error message?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 4, 2011, 11:43am UTC](https://community.unix.com/t/how-does-commandnotfound-work/296531/2 "2011-10-04T11:43:49Z")

</div>

Unless you've got something weird in your profile that runs python scripts every time you get a command not found error, I suspect that's a python thing and not a bash one.

---------- Post updated at 09:43 AM ---------- Previous update was at 09:40 AM ----------

First hit for 'not found' in man bash:

```nohighlight
       If the name is neither a shell function nor a builtin, and contains no
       slashes, bash searches each element of the PATH for a directory con-
       taining an executable file by that name. Bash uses a hash table to
       remember the full pathnames of executable files (see hash under SHELL
       BUILTIN COMMANDS below). 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.

```

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [October 4, 2011, 10:33pm UTC](https://community.unix.com/t/how-does-commandnotfound-work/296531/3 "2011-10-04T22:33:23Z")

</div>

Here is a good overview of command-not-found: [Bash: Handling Command Not Found | Linux Journal](http://www.linuxjournal.com/content/bash-command-not-found).

How it is actually implemented varies by distribution.

---

<div class="post-metadata">

**Author:** ![vistastar](https://community.unix.com/user_avatar/community.unix.com/vistastar/32/2003_2.png) [@vistastar](https://community.unix.com/u/vistastar)\
**Post date:** [October 5, 2011, 12:42am UTC](https://community.unix.com/t/how-does-commandnotfound-work/296531/4 "2011-10-05T00:42:56Z")

</div>

Very enlightening, thank you all.
