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 different exit status, depending on the problem. Often, this information can be found in the 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:

bash$ ls > /dev/null
bash$ echo $?
0
bash$



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

The first example will set the magic variable $? to 0, as shown (output of the ls command was redirected to /dev/null for brevity in the example). The second ...

Get FreeBSD® 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.