Home Linux Tips & Tricks How To Easily Correct Misspelled Bash Commands In Linux

How To Easily Correct Misspelled Bash Commands In Linux

By sk
Published: Last Updated on 5.5K views

When you entered a misspelled a Linux/Unix command, you would simply hit the UP arrow to bring up the command you just ran, and navigate to the misspelled word using the LEFT/RIGHT keys, and correct the word(s), finally hit ENTER key to run it again, right? That's what I was doing until I came across this trick. There is an easier way to correct misspelled Bash commands in Linux operating systems. This brief tutorial explains how to do it. Read on.

Correct Misspelled Bash Commands In Linux

Have you run a mistyped command something like below?

$ unme -r
bash: unme: command not found

Did you notice? There is a typo in the above command. I missed the letter "a" in the "uname" command.

I often did this mistakes a lot. Before I know this trick, I usually hit UP arrow to bring up the command and go to the misspelled word in the command, correct the spelling and typos and hit the ENTER key to run that command again. But believe me. The below trick is yet another way to correct any typos and spelling mistakes in a command you just ran.

To easily correct the above misspelled command, just run:

$ ^nm^nam^

This will replace the characters "nm" with "nam" in the "uname" command. Cool, yeah? This not only corrects the typo, but also runs the command. Check the following screenshot.

Correct Misspelled Bash Commands In Linux
Correct Misspelled Bash Commands In Linux

Use this trick when you made a typo in a command. Please note that it works only in Bash shell.

Automatically correct typos when using cd command

Have you ever wondered how to automatically correct spelling mistakes and typos when using "cd" command? No? It's alright! The following trick will explain how to do it.

This trick will only help to correct the spelling mistakes and typos when using "cd" command.

Let us say, you want to switch to "Downloads" directory using command:

$ cd Donloads
bash: cd: Donloads: No such file or directory

Oops! There is no such file or directory with name "Donloads". Well, the correct name of that directory was "Downloads". The "w" is missing in the above command.

To fix this issue and automatically correct the typos while using cd command, edit your .bashrc file:

$ vi ~/.bashrc

Add the following line at end.

[...]
shopt -s cdspell

After adding the above line, hit ESC key and type :wq to save and exit the file.

Finally, run the following command to update the changes.

$ source ~/.bashrc

Now, if there are any typos or spelling mistakes in the path while using cd command, it will automatically corrects and land you in the correct directory.

Automatically correct typos when using cd command
Automatically correct typos when using cd command

As you see in the above command, I intentionally made a typo ("Donloads" instead of "Downloads"), but Bash automatically detected the correct directory name and cd into it.

Fish and Zsh shells have this feature built-in. So, you don't need this trick if you use those shells.

This trick, however, has some limitations. It works only if you use the correct case. In the above example, if you type "cd donloads" instead of "cd Donloads", it won't recognize the correct path. Also, if there were more than one letters missing in the path, it won't work either.

You May Also Like

7 comments

iamstrong February 7, 2018 - 11:57 pm

Great!

Reply
MarkDubya February 9, 2018 - 7:54 am

Or you could just use thefuck: https://github.com/nvbn/thefuck

Reply
sk February 9, 2018 - 11:46 am

Yep, you’re right! Thanks for the suggestion. I will look into it.

Reply
AlmaGrebi February 10, 2018 - 3:43 pm

How to show the number of errors in Bash commands in Linux

Reply
sparc September 30, 2019 - 4:52 am

or use zsh and correct workd under cursor with ctrl-x c

Reply
Juan February 12, 2020 - 9:19 am

Thank you very much!

Reply
miksuh January 26, 2021 - 8:45 am

That cd trick could be nice feature, but unfortunately there is one big problem.

If you have eg directories like files 1 and files2 and if you then run command:

cd files

then you are moved to files2. That is not good. If there is two or more similar directories like that then that command really should not do that. It should not move you to some directory because you may be moved to wrong directory and you may not notice it. This problem may make this feature cause confusion and problems. And if you activate this for the root this could be dangerous or damage your system.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More