Unix: Tricks with history

dinosaur
Credit: flickr / Gareth Williams

I've heard that those who can't remember history are condemned to repeat it but for Unix users, repeating history is not only advantageous, it's something of a skill.

When working on the command line, Unix users are practically infamous for their fascination with saving a few keystrokes. One of the better ways to do this is to reuse previously entered commands in lieu of typing them again. While the most basic variety of command reuse is easy and popular, less well known options can take command resude to new levels of resourcefulness.

The easiest use of history is to simply repeat the previous command by typing !!. Since rerunning the most recent command is seldom all we want to do, we can also enter commands such as !14 and run whatever command is sitting in the 14th position in our history queue. Depending on the size of our history queue as determined by the HISTSIZE environment variable, this is likely be the 14th of the last 100 or the 14th of the last 1,000 commands. When determining which of a long series of commands one wants to repeat, it's often easier to simply hit the up arrow key until you see the one you want to reuse.

You can also use the !-# syntax (e.g., !-3) to rerun whatever command you ran # commands earlier. When you look at your history of commands or press the up arrow, what you'll see is the command you ran, however, not the !-2 that you used to run it.

And !! doesn't have to be the only thing you type on the command line. One of the more useful !! commands that I've seen comes into play when you type a command that requires root access and then type sudo !! to run the same command with root access. Another is when you want to save a particular useful command to a file by doing something like this:

echo !! > myscript

The other easy option is to enter something like !c. This command would repeat the last command you ran that started with a "c". Though a bit riskier (do you really know what command you last used that started with a "c" is the right one?), you can't get work done with many fewer keystrokes than that!

And, of course, you can list your past commands with the history command or the last eleven with history 11.

here history reuse gets a little more interesting is when you want to run a previously entered command, but with some changes. If the command you want to run with changes is the previous command -- as when you want to run the same basic command against multiple targets -- you can do something like this:

^this^that^

You might have just used ^is^at^ but it would depend on the content you're trying to change whether "is" would uniquely identify that part of the command you need to change.

If you've got a history buffer like I do on the servers you manage (1,000 lines), it sometimes makes a lot of sense to clear it out and start over. You can remove all remembered commands by typing history -c.

You can also reuse parts of commands that you've entered previously. This is where command history gets a little tricker. The string !$ represents the last word on the command you most recently entered. Here's an example:

$ echo Joy to the World
Joy to the World
$ echo !$
echo World
World

A more useful example might be when the last "word" (argument) in a command is the full path for a file such as:

$ cp somebigfile /data/archive/hqs/sombigfile
$ gzip !$

And you can use both the !^ and the !$ in the same command if you are so inclined.

$ echo this that
this that
$ cp !^ !$
cp this that

Similarly, you can reuse the first argument in a command with the string !^.

$ echo one two three
one two three
$ touch !^
touch one

t's also possible to reuse other parts of a command than the first and last arguments. In the command below, what we're doing is using the echo command to display the third argument from the last echo command that we entered. This syntax is not as easy to remember as !^ and !$, but might still come in handy and I'm glad there's some way to do this.

$ echo one two three four five six seven
one two three four five six seven
$ echo !echo:3
echo three
three

One additional thing that you can do is rerun the last command that contains a particular string, whether or not the command starts with that string. Instead of typing something like !echo, you would type !?string as in this example:

$ !?five
echo one two three four five six seven
one two three four five six seven

The history utility has long allowed users to make good use of previously entered commands, but is more flexible than many of us have likely realized.

This article is published as part of the IDG Contributor Network. Want to Join?

To express your thoughts on Computerworld content, visit Computerworld's Facebook page, LinkedIn page and Twitter stream.
Related:
Windows 10 annoyances and solutions
Shop Tech Products at Amazon
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.