Appendix B. Useful Commands

The commands on your system form the building blocks that you can glue together in your scripts. The following sections cover some of the more useful commands from a scripting point of view, divided into related sections. The listings here appear in a brief format. As always, however, you can find detailed information on these and other commands by perusing the online documentation.

Because of differences between Unix, Mac OS X, Linux, and the Cygwin environment on Windows, the listings here focus on the most common options for these commands. As always, use the ever-handy online manuals to look up the full documentation on each command.

Navigating the System

These commands help you interact with the operating system.


exit exit_code


Exits the current shell. You can pass an optional exit code, a number. An exit code of 0 (zero) indicates the script executed successfully. A nonzero value indicates an error.


$ exit

Exits the current shell.




file options filename


Attempts to classify the type of each file passed on the command line. Usually, the file command does this by reading the first few bytes of a file and looking for matches in a file of magic values, /etc/magic. This isn't really magic but simple comparisons. For example, the file command should be able to determine ASCII text files, executable programs, and other types of files.

The file command does not always classify correctly, but it usually does a good job. ...

Get Beginning Shell Scripting now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.