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.