Bash command method for correcting spelling errors in Linux, linuxbash

Source: Internet
Author: User

Bash command method for correcting spelling errors in Linux, linuxbash

I know that you can press the up arrow to call up the command you have run, use the left/right-click to move to a misspelled word, correct the misspelled word, and press enter to run it again, right? But wait. There is also a simpler way to correct Bash commands with incorrect spelling in GNU/Linux. This tutorial explains how to do this.

Bash command for correcting spelling errors in Linux

Have you ever run an incorrect input command similar to the following?

$ unme -rbash: unme: command not found

Have you noticed this? The preceding command has an error. I have missing the letter a in the uname command.

I have made this stupid mistake many times. Before I knew this technique, I used to press the up arrow to call up the command, convert it to a misspelled word in the command, correct the spelling mistake, and press the Enter key to run the command again. But believe me. The following tips help you easily correct any spelling mistakes in the command you just run.

To easily correct the spelling mistake, run the following command:

$ ^nm^nam^

This will replace nm with nam in the uname command. Cool, right? It can not only correct the typos, but also run commands. View the following.

Use this technique when you enter an error in a command. Note that it only applies to Bash shell.

Additional tips:

Have you ever wondered how to automatically correct spelling errors when using the cd command? No? It doesn't matter! The following tips explain how to do this.

This technique can only correct spelling errors when using the cd command.

For example, you want to use the following command to switch to the Downloads directory:

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

Oh! There is no file or directory named Donloads. Yes. The correct name is Downloads. W is missing from the above command.

To solve this problem and automatically correct the error when using the cd command, edit your. bashrc file:

$ vi ~/.bashrc

Add the following lines.

[...]shopt -s cdspell

Enter: wq to save and exit the file.

Finally, run the following command to update the changes.

$ source ~/.bashrc

Now, if there are any spelling errors in the path when using the cd command, it will automatically correct and enter the correct directory.

As you can see in the preceding command, I intentionally entered an error (Donloads instead of Downloads), but Bash automatically detected the correct directory NAME AND cd entered it.

Fish and Zsh shell have built-in functions. Therefore, if you are using them, you do not need this technique.

However, this technique has some limitations. It only applies to correct case. In the above example, if you enter cd donloads instead of cd Donloads, it cannot identify the correct path. In addition, it does not work if multiple letters are missing from the path.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.