05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

### aliases ###<br />

alias ls="ls -FG"<br />

alias la="ls -FGA"<br />

alias ll="ls -FGAl"<br />

### functions ###<br />

function pman() {<br />

man -t "${1}" | open -f -a /Applications/Preview.app/<br />

}<br />

Again, the lines that begin with # are comments.<br />

Creating Commands with Aliases and Functions<br />

The preceding sample .bashrc file shows a few aliases and a function. Essentially, these are both<br />

ways to create simple command-line commands that carry out more complex commands. In general,<br />

an alias is a way of creating a shortened version of a single command, while a function<br />

creates a command out of an actual shell script that may consist of many commands and simple<br />

logic.<br />

Aliases are incredibly handy when you find you are using the same commands over and over<br />

and want to simplify them; you can even use them to essentially overwrite the default behavior<br />

of a command. Take the first alias listed previously:<br />

alias ls="ls -FG"<br />

This command creates the ls alias from the ls -FG command, effectively overwriting ls so<br />

that it will, by default, provide colored output and the benefits of the -F flag as well. The other<br />

two aliases in my .bashrc file also create aliases to other common ls options.<br />

NOTE You can create aliases at the command line using the alias command as well—<br />

however, if you don’t save these to your .bashrc file, they will be gone once you exit that<br />

particular shell.<br />

Functions, on the other hand, are more complex, as they are essentially simple shell scripts.<br />

Here’s an example from the preceding .bashrc file:<br />

function pman() {<br />

man -t "${1}" | open -f -a /Applications/Preview.app/<br />

}<br />

The first line defines pman as a function accessible from the command line, and everything<br />

between the curly braces defines what happens when we use the pman command. So, if I were to<br />

enter pman ls at the command line, the function would take the first argument, ls, and run<br />

man -t ls | open -f -a /Applications/Preview.app/, thus allowing me to simply open the man<br />

page of any command in the Preview application in one simple command.<br />

Summary<br />

CHAPTER 18 INTRODUCING DARWIN AND THE SHELL 327<br />

Whew, there was lots of info in this chapter. While the aim wasn’t to teach you everything about<br />

working with Darwin (which would be an entire book in itself just to scratch the surface), hopefully<br />

you now know enough to get around comfortably in Darwin, and more importantly, have

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!