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 $?
0

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

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.