It depends on the environment, but I'd say it's poor style.
Unix-like systems have a strong convention that an exit status of 0 denotes success, and any non-zero exit status denotes failure. Some, but not all, programs distinguish between different kinds of failures with different non-zero exit codes; for example grep
typically returns 0 if the pattern was found, 1 if it wasn't, and 2 (or more) if there was an error such as a missing file.
This convention is pretty much hard wired into Unix shells. For example, in sh
, bash
, and other Bourne-like shells, the if
statement treats a 0 exit status as success/true, and a non-zero exit status as failure/false:
if your-command
then
echo ok
else
echo FAILURE
fi
I believe the conventions under MS Windows are similar.
Now there's certainly nothing stopping you from writing your own program that uses unconventional exit codes, especially if nothing else is going to interact with it, but be aware that you're violating a well established convention, and it could come back and bite you later.
The usual way for a program to return this kind of information is to print it to stdout
:
status="$(your-command)"
echo "Result is $status"