This content originally appeared on DEV Community and was authored by feng wei
Boolean logic is quite confusing when it comes to someone like me. I spent some time to figure it out and hope you can be beneficial from the post.
- Exit codes
True and false are common but also critical concepts to Boolean. Actually, each command you run on Linux will generate an "exit code", or "return code". In short, a true command generates an exit code of value "0", while a false command generates an exit code of "1". Specially, sometimes you may run a wrong command because of typo error and get the output "command not found". It indicates that the command doesn't exist on the system. For this case, the exit code will be "127".
- Viewing exit codes -- echo $?
The "$?" string store the exit code of last command, and the "echo" command will show the value
Example for a true command:
$ echo Hello
Hello
$ echo $?
0
Example for a false command:
$ mkdir /data
mkdir: cannot create directory ‘/data’: Permission denied
$ echo $?
1
Example for a non-exist command:
$ weekendiscoming
weekendiscoming: command not found
$ echo $?
127
- Case study:
Programs can respond differently based on different inputs and parameters with Boolean expressions and conditionals.
Considering the following syntax:
$ Test_condition && True_command || False_command
Let's input "true" and "false" into the commands:
$ true && true || false
$ echo $?
0
If the "Test_condition" command executed successfully, which means the exit code is 0, it would run "True_command".
$ false && true || false
$ echo $?
1
If the "Test_condition" command failed to execute with an exit code value of 1, it would run "False_command".
This content originally appeared on DEV Community and was authored by feng wei

feng wei | Sciencx (2024-08-16T09:21:35+00:00) Boolean: True and False. Retrieved from https://www.scien.cx/2024/08/16/boolean-true-and-false/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.