Every week the developers of the O’Reilly Network (that’s me, three
developers, and two admins) have a status meeting to check in with our key
managers, decide or rearrange priorities, and work through problems. This is a
dream meeting for managers, questions are answered and plans are laid.

It’s fair to say that status meetings aren’t a developer’s dream. After
several years of weekly meetings, ours were feeling stale. So we agreed to end
the meetings with a round of tips and tricks. First up, bash tricks.

At the end of our meeting, the managers bailed and we stuck around to geek out
over the tricks that make our work easier. Everyone had something to
contribute. I can’t recommend this enough! I’ve learned a lot from books like
Unix Power Tools. But by
sharing directly with your coworkers you get advice that’s targeted directly
to the work you do.

Here were our gems, the most useful tips that weren’t already common
knowledge among the developers.

pushd/popd

Bash will keep a history of the directories you visit, you just have to ask.
Bash stores the history in a stack and uses the commands pushd
and popd to manage the stack.

pushd dir - move the current directory onto the stack and change to
the dir directory.

popd - pops the top directory off of the stack and moves you into it.

We’re opening files all over the file system, internal code, vendor code, templates, configuration files, logs. Because of this we like the ability to take a detour on the file system and still navigate back to our working directory of the day. I think these commands are so useful that I alias’d them in my .bashrc


alias cd="pushd"
alias bd="popd"

Now the cd command manages the stack for me as well as changing directories. Aliasing popd to bd is an easy to remember and easy to type way to move back up the stack, think “change dir” and “back dir”.

History

Bash keeps a history of the commands you’ve run. My group was already comfortable with the up and down arrows to navigate the history, !! to repeat the last command, and !foo to repeat the last command starting with foo.

Our newest admin had a better way, CTRL-R. That does command auto completion. Repeatedly pressing CTRL-R lets you tab through matching commands.

Home/End

CTRL-A takes you to the beginning of the line and CTRL-E takes you to the end of the line. This is probably basic shell knowledge, but I’m probably (hopefully) not the only person who didn’t know it.

For Loops

We’ve got a cluster of machines that we’ll sometimes need to loop through. Here’s an example from our admins that checks uptime across our cluster.

$ for s in `cat server.list`; do ssh $s uptime; done;

Working with the Previous Command

Sometimes you want to run several commands on the same file, like run ls before deciding if that’s the file you want to edit.


ls -l /long/path/to/file.txt
vi /long/path/to/file.txt

Bash provides a shortcut (!$) that holds the last word from the previous command. So in the above you could just write vi !$.

If your last command had a typo you can fix the command and rerun it with this construct, ^foo^bar. That replaces the first occurrence of foo in your command with bar.

Bonus Tip: use Awk

Our admins seem to think awk is pretty useful. And my boss thought it was
so useful, he wrote a book
on it. I can’t keep any of the awk syntax in my head beyond printing out a
column from a file.

The normal column delimiter is whitespace. So if you wanted to print out the seventh column in an Apache access log (that’s the request url in my logs) you could write:

cat access_log | awk '{print $7}'

You can change the delimiter with -F. So if you wanted to list all the users on your system, you could pull them out of /etc/passwd with:

$ cat /etc/passwd | awk -F: '{print $1}'

The /etc/passwd delimiter is :, which I’ve indicated
to awk with -F:.

We’re doing vim tricks next.

What’s your favorite bash trick? How else do you share tricks among developers?