Home  >  Article  >  System Tutorial  >  Correct Bash command typos in Linux

Correct Bash command typos in Linux

WBOY
WBOYOriginal
2024-06-03 13:06:59610browse

I know you can press the up arrow to bring up the command you ran, then use the left/right keys to move to the misspelled word and correct the misspelled word, and finally press enter to run it again, right? ? But wait. There is an easier way to correct misspelled Bash commands in GNU/Linux. This tutorial explains how to do this. Please read on.

Correction of misspelled Bash commands in Linux

Have you ever run an incorrectly entered command similar to the one below?

$ unme -r
bash: unme: command not found

Did you notice? There is a bug in the above command. I'm missing the letters a in the uname command.

I have made this stupid mistake many times. Before I knew this trick, I used to press the up arrow to bring up a command, go to the misspelled word in the command, correct the spelling, and then press enter to run the command again. But trust me. The following tips are very easy to correct any typos in the command you just ran.

To easily correct the misspelled command above, just run:

$ ^nm^nam^

This will replace nm with nam in the uname command. Cool, huh? Not only does it correct typos, but it also runs commands. Check out the screenshot below.

在 Linux 中纠正 Bash 命令拼写错误

Use this trick when you make a typo in a command. Note that this only works with the Bash shell.

Extra Tips:

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

This tip only corrects spelling errors when using the cd command.

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

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

oops! No file or directory named Donloads. Yes, the correct name is Downloads. w is missing from the above command.

To resolve this issue and automatically correct errors when using the cd command, edit your .bashrc file:

$ vi ~/.bashrc

Add the following lines at the end.

[...]
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 typos in the path when using the cd command, it will automatically correct it and go to the correct directory.

在 Linux 中纠正 Bash 命令拼写错误

As you can see in the command above, I typed it incorrectly on purpose (Donloads instead of Downloads), but Bash automatically detected the correct directory name and cd Enter it.

Fish[1] This functionality is built into the Zsh shell. So, if you're using them, you don't need this trick.

However, this technique has some limitations. It only works if correct case is used. In the example above, if you enter cd donloads instead of cd Donloads, it will not recognize the correct path. Also, it doesn't work if there are multiple letters missing from the path.


The above is the detailed content of Correct Bash command typos in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn