bash

first post: how the shell made me go !h:7 !!

Ethan's picture
Tags: 

Hi I'm Ethan, the new guy. This is my first post, but beyond that brief bio and the unequivocal denial of any claims that I don't change my shirt on a daily basis, this post isn't going to be about me.

It's going to be about the totally off the hook tricks I leaned about the "previous command" shell shortcuts I learned today from this post and how they totally rocked my world a second ago.

So here's the situation: I downloaded a file and couldn't remember whether it was a zip or a tarball, so I ran this command:

tar -xzf ~/downloads/google-sitemap-generator.3.0b7.zip

Why did I try to unzip a tarball, you ask? Because I'm lazy, and when autocomplete says "press enter", I ask "how fast"? Needless to say, the result was some mumbo-jumbo that I'm sure must have had ample amounts of bit-level impatience buried somewhere underneath it.

Remembering the tricks from that post I tried this:

unzip !^

I was thinking that maybe the first argument (referenced by !^) would be recognized as the argument after the flags. But no go, it resolved to unzip -xzf

Now I had a bit of a dilemma: I couldn't use any more of the cool tricks because they all referred to the command prior to the last...unless. What if all the argument referencing commands could be prefixed with a pattern!?! Sure enough, this gem worked like a charm...gem...:

unzip !tar:2

Which basically reads unzip {the second argument of the last command matching the glob-string 'tar'}

To my total, complete astonishment it actually worked. Brilliant.