8

I'd like for something to hook into the 'command not found' error on bash and offer a useful fix.

For example:

grep-C3  error
bash: grep-C3: command not found

It would be nice if it said:

Did you mean: grep -C3 error (Y/n)

I've seen people include common typos in the .bash_profile script like 'alias gerp=grep', but I figure someone has probably written a general extension to bash to fix this sort of thing.

Has anyone heard of such an extension?

1

4 Answers 4

4

I don't know of a general purpose one, but there is one for directory names:

$ shopt -s cdspell
$ cd /usr/ibn
/usr/bin
$ pwd
/usr/bin
$ cd /usr/shar/doc
/usr/share/doc

This is from the man page for Bash:

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

It might be possible to use this to do what you want, but the code would be quite a challenge. By the way, Ubuntu, for example, uses this to display packages that contain the command that was not found with installation instructions (apt-get).

3

Looks like shopt would be a good start.

Also, it appears that zsh has typo correction built in.

1
  • link to "shopt" looks like a blank page from here.
    – sylvainulg
    Oct 15, 2014 at 14:51
0

the command-not-found package should provide this feature in recent distros.

If not, you can invoke it manually from the command_not_found_handle() function.

Another alternative is this.

0

There's another tool (with a rude name that can be aliased to fixthis), here's what it does for the posted example:

kai@PC:~$ grep-C3 error
-bash: grep-C3: command not found
kai@PC:~$ fixthis
grep -C3 error [enter/↑/↓/ctrl+c]

I only just installed it (in a moment of frustration with my job, frustration unrelated to the Unix CLI), so I don't know how good it is.

It does seem a bit slow so far, but it works.

Instructions here: https://ostechnix.com/correct-errors-in-linux-commands/

Summary for install (name of tool corrected to avoid profanity and offense, so replace the first 'x' below with a 'u'):

pip3 install --user thefxck
emacs ~/.bashrc  # add alias
source ~/.bashrc
grep-C3 error
fixthis

You must log in to answer this question.

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