Bash/shell - cheat sheet

By: John McFarlane <john.mcfarlane@rockfloat.com>
Last updated: 08/08/2008 @ 01:45

Abstract:
This howto is simply a place for me to keep all the little bash tips and tricks that I need and use. I hope you find them valuable, please let me know if you have changes/fixes/additions. My goal is to add one tip every few weeks.

Tip This howto is not exclusive to Bash. The goal is simply to have a single place to document all the things I forget and need a quick reference for. Because of this you'll also find tips on things like Awk, Sed, Cut, and Sort - but the bulk will probably be Bash.




1. Process substitution

Bash can create special files from the output of commands. This takes a little getting used to, but can be extremely valuable. Here are a few examples:

# Cat out the current directory listing
user# cat <(ls)

# Cat out the directory listing on another computer
user# cat <(ssh some-computer ls)
    
Now you're probably thinking that's not so special. Here's something a little more interesing:

# Graphically compare a directory listing on two computers
user# meld <(ssh foo ls) <(ssh bar ls)

# Graphically compare /etc on two computers
user# meld <(ssh foo tree /etc) <(ssh bar tree /etc)
    
I'm finished with this step

2. Brace expansion

If you ever find yourself typing commands that use long file system paths as arguments, think about using brace expansion:

# Create a backup of xorg.conf
user# sudo cp /etc/X11/xorg.conf{,.BACKUP}

# Do a visual diff between your PostgreSQL config and a backup
root# meld /var/lib/postgresql/data/pg_hba.conf{.ORIG,}
    
I'm finished with this step

Changelog: Date Description
08/08/2008 @ 01:45 Initial creation

This document was originally created on 08/08/2008


Conventions and tips for this howto document:
  1. This howto revolves around the Bash shell. If you are using something like sh, zsh or whatever you probably want to be looking somewhere else.

Disclaimer:
This page is not endorsed by gentoo.org or any other cool cats. Any information provided in this document is to be used at your own risk.