[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Off topic: non zero error codes



Erik Josefsson writes:

Good morning,

I have wanted to ask for a long time about something I made up from hearsay
whether it is remotely true. I hope that's OK.

Is there is a shell/language where the returncode for TRUE is zero and that
that is the opposite of how all other shells/languages are made?

Yes, there is. It is called POSIX shell. Other shells have adpoted the
concept as well, e.g. Bash.

Try:

	$ true
	$ echo $?
	0

	$ false
	$ echo $?
	1

The argument for creating it was that it then becomes handier to say "no
errors, one error, two errors..." than to say "success, one error, two
errors...".

I think it stems from two different traditions:

* One is to use ``error codes'' to show that something went wrong and 0 if
  everything is OK.

* The other is to have a function report ``true'' (commonly 1) on success and
  ``false'' (commonly 0) on error.

One can see both traditions used in different styles of C-programs -- some
more advanced programming languages provide the concept of  Exceptions to
avoid these situations, although it is not always consistent even in
comparably new programming languages like Java.

For program execution from the shell, the ``error codes''-idea is applied
and as `true` and `false` are just programs which can be run from the POSIX
shell, the counter-intuitive use of `true` ~ 0 and `false` ~ 1 can be
explained.

HTH
LInux-Fan

[...]

Attachment: pgpsTOgjgyfxM.pgp
Description: PGP signature


Reply to: