Boolean: True and False

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 y…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Boolean: True and False." feng wei | Sciencx - Friday August 16, 2024, https://www.scien.cx/2024/08/16/boolean-true-and-false/
HARVARD
feng wei | Sciencx Friday August 16, 2024 » Boolean: True and False., viewed ,<https://www.scien.cx/2024/08/16/boolean-true-and-false/>
VANCOUVER
feng wei | Sciencx - » Boolean: True and False. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/16/boolean-true-and-false/
CHICAGO
" » Boolean: True and False." feng wei | Sciencx - Accessed . https://www.scien.cx/2024/08/16/boolean-true-and-false/
IEEE
" » Boolean: True and False." feng wei | Sciencx [Online]. Available: https://www.scien.cx/2024/08/16/boolean-true-and-false/. [Accessed: ]
rf:citation
» Boolean: True and False | feng wei | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.