Since I’m taking this blog thing a lot more serious now, I’ll devote a quick blog post to one of the things I love more about the Bourn Again SHell: history.

Working in a Linux environment inevitably makes you use a shell (i.e. the actual brain behind the famous “temrinal”). There are many shells one could use but I’ll risk saying bash is the most common. In all fairness, if you are any sort of coder/developer/debugger/hacker/script-kiddie/indiana-jones-of-the-computer-world you’ll eventually fall in love with it. It’s also worth mentioning that most people (me included), they never “sat down” to learn bash. We just started copy&pasting commands we saw somewhere, then you start reading whatever you’re posting, then you learn to use manpages (or equivalent), etc.. 5 years later you certainly “know your ways”.

For all bash/shell related problems/questions, there are probably 5 different ways around it. That said, it’s common to stick always to the same tools/methods for solving all problems…

“this way works, so why learning anything new?”

Basically, Time! The more culturally aware you are in terms of bash & bash scripting; the quicker you’ll solve your problems.

Finally, after all this obvious blogish talk, let’s get to the point:

[sourcecode language=”shell”]
history
[/sourcecode]

That command shows all the previous commands you (your user) executed. Ages ago I learned to use it with grep to see what command I had executed that had *string* on it.

[sourcecode language=”shell”]
history | grep g++
[/sourcecode]

This one’s really useful and if that’s new to you, this post is already worth it’s effort. But that one’s clumsy,
you’ll probably end up copying & pasting something by using that filthy rat of yours or by petting that flat life-less thing on
your notebook – that’s not efficient.

By running that command you’ll see that there’s an index for each command executed; for example:

[sourcecode language=”shell”]
#lol.. those are the oldest entries on my history (5k commands long)
cpscotti@clovis-laptop:~$ history
1  cd push-snowboarding/
2  git status -s
3  cd PushBurton2
4  ls
5  git status -s
6  git add *.cpp *.h

[/sourcecode]

That index is there for a very nice reason; History Expansion. By entering !index, you’ll run that line again. Suppose I want to
run an old command again ( e.g. meego-sb-session start ) which is hard to remember or too long to type, one can just find out the
index for the last occurrence of that command and.. voila!

[sourcecode language=”shell”]
[sbox-HARMATTAN_X86: ~] > history | grep start
167  history | grep start
168  meego-sb-session start
501  meego-sb-session start
503  history | grep start
[sbox-HARMATTAN_X86: ~] > !168
meego-sb-session start

[/sourcecode]

But hey, if you are still lazy to type in those two lines.. we can go further!
There’s another command I execute quite often that’s just a PITA to remembering/type..:

[sourcecode language=”shell”]
cpscotti@clovis-laptop:~$ Xephyr :2 -host-cursor -screen 854x480x16 -dpi 96 -ac +extension Composite &
[/sourcecode]

Diving a bit more into history‘s manpages you’d find the ” ? “and a lot more!
I won’t explain much, just watch! 😀

[sourcecode language=”shell”]
cpscotti@clovis-laptop:~$ !?Xep?
Xephyr :2 -host-cursor -screen 854x480x16 -dpi 96 -ac +extension Composite &
[/sourcecode]

Of course you have to “know your past” otherwise you’ll end up executing bizarre commands but well.. that works great for me!

Enjoy!

 

Oh, one last thing! You can “pimp up” your history’s size! I think Ubuntu’s default is 2000.. way to small in my opinion! Just edit HISTSIZE inside your .bashrc script!

(in doubt check manpages for bash and history!)

« »