If you are using Linux or a Mac these days, then you likely have bash as your default shell. It generally comes with a few nice features (tab-completion, history etc.), but there are a few tips and tricks which will make it much nicer to use. Here’s a run down of my favourite 5.
These tips are to be added to your .bashrc file. To open this use
where you can replace nano with your favourite text editor (vi, emacs, pico, gedit, kate etc.)
Search through history effectively
Type control-R to start searching, then type a phrase to find it in your history. This tip isn’t advanced, but it’s a starting point that everyone should know!
history | grep "searchterm"
Will find that term in your history in a slightly more flexible way.
Improve the bash history
If you use two bash sessions at the same time, which ever you close second will write over the commands you’ve typed in the first one. That means that your history is lost for one of the sessions. Annoying to say the least. Luckily there is a way to fix this.
Open your .bashrc file as detailed above and add the lines:
shopt -s histappend
This will append any new history to the existing file rather than rewriting it, as well as adding to the history every time the prompt is shown. Now, all your history will be saved!
Fix common spelling mistakes
In your .bashrc file, add:
shopt -s cdspell
This will ignore simple mistakes such as typing otp instead of opt, or ect instead of etc.
Remove duplicates in your history
Ever gone through your history and found that it’s mainly ls, pwd and exit? How about when typing the same command over and over, such as reading out the contents of a log file.
will ignore duplicate values, as well as ignoring a few common commands.
Fix multiple line commands
shopt -s cmdhist
This will ensure that multiple line commands stay together in your history.
So, 5 things that have saved me time – if you have any other tips, let me know in the comments!