Great Bash Tricks

The UNIX command line is powerful. A lot can get done with just a few commands. That’s true whether you’re a novice or a pro. It’s a fantastic tool because it makes you more effective. It’s easy to get started, and easy to build upon your experience. As you get better, your efficiency increases. Here’s a collection of tips and tricks to help improve your command line experience.

pbcopy / pbpaste

The pbcopy and pbpaste commands, available in OS X, are used to interact with the system paste board. Other systems call it a clipboard. The ability to use the system clipboard from the command line is very useful. Let’s say you have two terminal windows, and you want them both to be in the same directory.

Without it, in the first window, you’ll run pwd to the display the current directory.

$ pwd
/path/to/the/directory

In the second terminal window. You’ll need to type the entire path for the cd command,

$ cd /path/to/the/directory

That works, but it’s a little slow. It’s also error prone, because it’s relies on the user having to type the path. It’d be better to use the system clipboard.

In the first terminal window

$ pwd | pbcopy

In the second terminal window

$ cd `pbpaste`

The cd command doesn’t read from STDIN. That’s why backticks are used to pass a value to cd. `

xargs

There are times when you have a vertical list of items, but want to create a horizontal list, so you can pass it to a command. That’s exactly what the xargs command does. Technically, xargs reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments. Here’s an example that will create a list of files, and open each one of them.

$ ls -1 *.pdf | xargs open

Directory Variables

Using variables is a great way to access directory paths. The longer the directory path, the more valuable the variable is. The first way to use variables is to manage them in your bashrc. Create a variable for each project directory path you use. Just the top level project directory should suffice. If you have project sub directories that you use often, create a variable for those too.

$ project='/www/sites/project/public_html'

Then to access the project variable, just type

$ cd $project

Another way to use variables is to create them adhoc on the command line. In a WordPress project, you might find yourself bouncing between your active theme, a plugin, and your wp-content directories. Change directory to your active theme, and

$  t=`pwd`

then to get back to your theme directory, you’ll use the $t variable as an argument for the cd command.

$ cd $t

Wrapper Functions

Creating bash functions is a great way to increase your efficacy. It’s hard to say what functions you should write. The best functions meet your needs. There are a few reasons to create functions in bash.

  • combine multiple related commands
  • encapsulate logic
  • simplify hard-to-type commands

When you do decide to create a bash function, be sure to put your required parameters toward the beginning, and optional parameters toward the end. Also, consider providing default values for your optional parameters. This can help make code perform more consistently.

Sorry, but comments are closed. I hope you enjoyed the article