Search notes:

Bash: $? exit status

$? stores the exit status of the last command.
In bash, an exit status of zero represents true and all non-zero values represent false. Therfore, the exit status of true is 0:
true
echo exit status of true is $?

false
echo exit status of false is $?

# Output
#
#  exit status of true is 0
#  exit status of false is 1
Github repository about-Bash, path: /parameters/special/exit-status/true-and-false.sh

C standard library

With the c standard library, the exit status can be set with the accordingly named exit() function.
The following program (prog_0) sets it to zero:
#include <stdlib.h>
int main() {
  exit(0);
}
Github repository about-Bash, path: /parameters/special/exit-status/prog_0.c
And this program (prog_1) sets it to one:
#include <stdlib.h>
int main() {
  exit(1);
}
Github repository about-Bash, path: /parameters/special/exit-status/prog_1.c
Bash's if statement decides the true or false value opon the evaluated command's exit status:
gcc prog_0.c -o prog_0
gcc prog_1.c -o prog_1

if ./prog_0; then
   echo prog_0\'s exit status is 0
else
   echo prog_0\'s exit status is not 0, it\'s $?
fi

if ./prog_1; then
   echo prog_1\'s exit status is 0
else
   echo prog_1\'s exit status is not 0, it\'s $?
fi

# Output:
#
#   prog_0's exit status is 0
#   prog_1's exit status is not 0, it's 1
Github repository about-Bash, path: /parameters/special/exit-status/run.sh

Index