Search code examples
bashshellsolarisopensolaris

Why does "false; echo $?" return 1, if the Solaris source for false exits with 255?


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);
}

Solution

  • 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.