📜 ⬆️ ⬇️

Bash hints and tips

For the uninitiated, bash is the default shell for many Linux distributions, including Fedora, Ubuntu, Redhat, and others. Using the Linux operating system, you probably use bash as well. For this reason, below I have collected a few frequently encountered problems with bash and simple ways to solve them.

1. Loss of command history

If you open a terminal window and enter several commands, and then open a second window, the history of bash commands in the second window will not contain commands from the first. In addition, if you close the first terminal, and then the second, the command history from the first terminal will be overwritten by the second. It is doubly unpleasant!
')
This is due to the fact that the command history is recorded only when the terminal is closed, and not after each command. It can be fixed.

To do this, you need to edit the .bashrc file (for beginners: any file that starts with a dot is hidden — it serves to store user settings).

nano ~/.bashrc

sudo is not needed here, it is a user file, not a system setup. I like nano, but you can use any editor: gedit, kate, mousepad, vi or emacs

add lines

shopt -s histappend

PROMPT_COMMAND = 'history -a'

And save. (Ctrl-O for recording. "^" Means "Ctrl" key in nano and other programs. ^ X to exit)

So the command history will be added to the old one, rather than overwriting it, and the recording will occur every time the bash hint is displayed.

2. Stupid spelling mistakes

Add

shopt -s cdspell

again in .bashrc. Thus, spelling errors (for example, ect instead of etc) will be corrected.

3. Duplicates in command history

I often type cd .. several times in a row, and when I press the "up" key to return to the previously entered commands, I do not want to be constantly reminded of my suboptimal movements through the file system.

Add

export HISTCONTROL="ignoredups"

again to .bashrc.

Or even better, add

export HISTIGNORE="&:ls:[bf]g:exit"

This will get rid of duplicates, as well as ls , bg , fg and exit , which will make the history of bash commands more readable.

4. Broken multi-line commands in command history

Add

shopt -s cmdhist

in .bashrc, and multi-line commands will be written in one line, which makes editing easier.

5. A couple of additional tips from commentators.

Press Ctrl-R in bash, then start typing the command. So search in the history of commands is much faster than clicking 300 times "up."

You can also search using

history | grep "foo"

Team

cd -

returns to the last visited directory - it is useful when you need to go somewhere to change something, and then quickly go back.

Pressing "Esc" + "." calls the last object mentioned. For example, if you type cat /etc/apt/sources.list and then rm and press "Esc" + ".", Then the command will be automatically completed: rm /etc/apt/sources.list . Try it!

Conclusion

Here are some tips for making bash command history more convenient. If you have any tricks on this subject, add in the comments!

Via Linux to digg in Russian

Source: https://habr.com/ru/post/31326/


All Articles