Exit Status

Most programs in FreeBSD return an exit status when they terminate. The exit status is usually 0 if successful, and some number other than 0 if something went wrong. Some programs will return a general error of 1, and some will return a different number depending on the particular problem. The meanings of specific error codes can usually be found in each program’s man page.

The exit status of the last program that ran is stored in the magic variable $?. Here are a couple of examples:

# ls > /dev/null
						# echo $?

# ls -2 /dev/null
ls: illegal option -- 2
usage: ls [-ABCFGHLPRTWabcdfgiklnoqrstu1] [file ...]
# echo $?

The first example, a command that exits normally, will set the magic variable $? to 0 (the output of the ls command ...

Get FreeBSD6 Unleashed now with O’Reilly online learning.

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