… And a (Wo)Man Page

Post navigation

Because of the lonely way I learned shell programming (re-purposing and adapting code from others with extensive stack-overflow research), everything I know has to do with script writing. So, recently, while preparing for my first time teaching others about the shell, I had a chance to discuss command-line magic with a more experienced coder, Stephen Turner, and he told me about two basic shortcuts that I did not know.

1. Quickly Subsitute Strings to Adapt Previous Commands

What if I got ahold of a file containing a list of all the names of every student that ever attended Hogwarts (one per line) and I wrote a long command to discover how many students had “Neville” somewhere in their name?

$cat hogwarts.txt | grep "Neville"| wc -l

And now I want to run pretty much the same command, but with a small change — perhaps I want to use the Gryffindor specific list instead. Like this:

$cat gryffindor.txt | grep "Neville"| wc -l

For years, I have used the up arrow and then scrolled to the start of the command in order to change it. But, no longer.
Instead, I can use the ^ caret (on the 6 key) to substitutegryffindor for hogwarts in the previous command, like so:

$^hogwarts^gryffindor
cat gryffindor.txt | grep "Neville"| wc -l

And Boom, the shell prints out the new command and starts running it!

2. Use History as More Than Just a Record of Your Commands

Did you know that history will bring up a numbered list of your command history? Well, I did. I did not know that you can use those numbers to reissue commands. How cool is that? Answer: So cool. Here’s how it works. First call up your history:

I recently certified as a Software Carpentry Instructor (more on that later), and when I went to look at the bios of the other instructors I discovered that Every. One. Had. A. Blog. So, I asked Stephen Turner whether this was a Thing I Need To Do. Well, since you are reading this, you know what he said. So Blogging. Here goes.