05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

296<br />

CHAPTER 18 INTRODUCING DARWIN AND THE SHELL<br />

NOTE cd is kind of special in that, unlike ls, it is not an executable file—rather, it is a special<br />

type of command referred to as a built-in command. This command, along with a few others<br />

you will learn about, is a function of the shell, and not traditionally a separate executable file.<br />

Although most common built-ins like cd exist in all common shells, it is possible that a built-in<br />

command in one shell will behave slightly differently than one in another (cd isn’t one of<br />

these—it behaves the same way in every shell I’ve used over the past 20 or so years).<br />

To issue the cd command, simply type cd followed by your destination, like so:<br />

<strong>Leopard</strong>:~ scott$ cd /<br />

<strong>Leopard</strong>:/ scott$<br />

If no argument is given, then cd will take you back to your home directory:<br />

<strong>Leopard</strong>:/ scott$ cd<br />

<strong>Leopard</strong>:~ scott$<br />

Finally, if cd doesn’t recognize the argument as a file or directory, it will tell you so:<br />

<strong>Leopard</strong>:~ scott$ cd /blah<br />

-bash: cd: /blah: No such file or directory<br />

<strong>Leopard</strong>:~ scott$<br />

This is an error statement, and most well-written functions and executables will provide<br />

some sort of error message if the information you provide doesn’t make sense to them.<br />

pwd<br />

CAUTION Just because well-written commands often give you an error message when you<br />

do something wrong, it’s important to note that this only happens when the command has no<br />

idea how to parse the information you’ve given it. However, this will not prevent all erroneous<br />

commands from executing. If a command is recognized as valid, even if the information you<br />

enter isn’t, the command will execute. While this is often harmless, it can have disastrous consequences<br />

(the rm command, as you will soon learn, deletes files immediately and<br />

permanently, and can cause all sorts of badness if used poorly).<br />

The final command in this section is the pwd command. The pwd command returns your current<br />

working directory, as this information is by default available in your prompt (after the :). You<br />

may not need to use this command too often—however, it is useful in illustrating the file structure<br />

of the system and can come in quite handy when you need to pass your current path into a<br />

script. Also, it’s possible that you may find yourself stuck in a foreign shell on a foreign machine,<br />

where you may actually need this. The basic pwd command looks like this:<br />

<strong>Leopard</strong>:~ scott$ pwd<br />

/Users/scott<br />

In this case, pwd returns the absolute path of my home directory rather than the abbreviated<br />

~ in the command prompt. This can come in handy on a foreign machine where you may not<br />

know where the home directory is located, or one where the path info isn’t part of the prompt.<br />

Being a rather simple command, pwd only offers two extra options:<br />

• pwd -L will print the logical path to your working directory.<br />

• pwd -D will print out the physical path to your working directory resolving any symbolic<br />

links. This is the default behavior of pwd.

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

Saved successfully!

Ooh no, something went wrong!