3

I needed to find the source code for the implementation of the false.
I found sources on github and found false.c where false exits with code 255.
So, why does "false; echo $?" return 1 in shell instead of 255? I think there is a source somewhere that I missed.

code from false.c file:

#pragma ident   "%Z%%M% %I% %E% SMI"

#include <unistd.h>

/*
 * Exit with a non-zero value as quickly as possible.
 */

int
main(void)
{
    _exit(255);
    /*NOTREACHED*/
    return (0);
}
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • It's not clear how Illumos is related to Solaris. A more obviously Solaris-esque source would be https://github.com/kofemann/opensolaris/blob/master/usr/src/cmd/false/false.c – tripleee Nov 02 '20 at 16:19
  • 2
    BTW, you can use `type false` to ask bash where the command `false` comes from, to determine whether it's defined by an external executable, a shell builtin, an alias, a function, etc. – Charles Duffy Nov 02 '20 at 16:20
  • 4
    POSIX merely mandates that `false` must return a non-zero exit code. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/false.html – tripleee Nov 02 '20 at 17:08

1 Answers1

13

If bash is your shell, false is a builtin -- so you're looking at the wrong source code.

See instead the version built into bash itself, in the file builtins/colon.def:

/* Return an unsuccessful result. */
int
false_builtin (ignore)
     char *ignore;
{
  return (1);
}

If you want to use your OS vendor's version of false instead of the built-in one, you can do that with command false or /bin/false.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Oh sure, I got wrong. And I got another question. I tried `type false` as you said on comment and got `false is a shell builtin` also `type /bin/false` writes `false is /bin/false`. Are there 2 falses or is it just one? – So_actually_we_did_it Nov 02 '20 at 17:45
  • 2
    Two: There's one built into the shell, and there's one provided by your operating system as an external binary. (If you have more than one shell installed on your system, they each may have a different built-in `false`; the only guarantee if it's a POSIX-compliant shell is that the behavior will comply with https://pubs.opengroup.org/onlinepubs/9699919799/utilities/false.html, specifying that it must return a "non-zero" value). – Charles Duffy Nov 02 '20 at 17:47