True and false are common concepts in all forms of computing. They\u2019re critical to Boolean logic after all, but did you know that true and false are also commands on Linux? Do you know how to use them?\nThe simplest explanation is that the true command generates an exit code of 0 and that the false command generates an exit code of 1. This explanation, however, doesn\u2019t provide much detail on how these commands can best be used.\nIn this post, we\u2019ll look at how the true and false commands work and how you might put them to use on the command line or in your scripts.\nViewing exit codes\nFirst, it\u2019s important to remember that a successful exit code (a.k.a. \u201creturn code\u201d) on Linux systems is 0. Think of this as meaning \u201czero errors\u201d. Exit codes indicating some form of failure will have values of 1 or greater.\nAnytime you run a command on Linux, an exit code is generated. While you\u2019ll see the expected output or error messages, you\u2019ll only see the exit codes if you ask. To ask, you just need to use the command echo $?. The $? string represents the exit code and the echo command will display the code like this:\n$ echo hello\nhello\n$ echo $?\n0\n\nThe command echo hello was successful, so the exit code is 0.\nHere\u2019s a simple example of displaying an exit code when a command is not successful. Hit a bunch of random keys on your keyboard, and you\u2019ll end up seeing something like this:\n$ asjdlkjdad\nbash: asjdlkjdad: command not found...\n\nAsk to see the exit code immediately afterwards and you\u2019ll see this:\n$ echo $?\n127\n\nAn exit code of 127 indicates that the command you just typed doesn\u2019t exist on the system.\nHere\u2019s another example in which we try to display a file that doesn\u2019t exist:\n$ cat dhksdfhjksfjhskfhjd\ncat: dhksdfhjksfjhskfhjd: No such file or directory\n$ echo $?\n1\n\nThe 1 exit code is something of a general exit code and will be returned for a variety of errors.\nUsing true and false\nThe simplest way to see how the true and false commands work on the command line is to run these commands:\n$ true; echo $?\n0\n$ false; echo $?\n1\n\nAs you can see, true simply returns a 0 and false simply returns a 1.\nProbably the most common use of the true command is to start an infinite loop. Instead of starting a loop with a command like while [ $num -le 12345678 ], you can use while true and the loop continues until you stop it will a ^c.\nwhile true\ndo\n echo \u201cstill running\u201d\n sleep 10\ndone\n\nA while false loop would fail immediately. However, another way to start an infinite loop is to use syntax like this:\nuntil false; do\n echo still running\n sleep 10\ndone\n\nWhen you halt an otherwise infinite loop by typing ^c and then check the return code, you should see this:\n$ run-forever\nstill running\nstill running\n^C$ echo $?\n130\n\nThe 130 exit code confirms that the loop was terminated with a ^c.\n$ more run-forever\nuntil false; do\n echo still running\n sleep 10\ndone\n\nIf you want a command to result in a successful exit code even if the command itself fails, you can pipe its output to true like this:\n$ cat nosuchfile | true; echo $?\ncat: nosuchfile: No such file or directory\n0 then\n> echo This command always runs\n> fiThis command always runs\nif false\n> then\n> echo This command never runs\n> fi\n$\nAs you see, the if false command has no output because the echo command isn\u2019t run.\nUsing a colon in place of true has the same effect as using true. Here\u2019s an example:\n$ if :\n> then\n> echo This command always runs\n> else\n> echo This command never runs\n> fi\nThis command always runs\n\nIt\u2019s also possible to set your own exit codes. For example, if you have the command exit 111 in a script as shown in the example below, the exit code for the script will be 111.\n#!\/bin\/bash\n\nif [ $# == 0 ]; then\n echo \u201c$0 filename\u201d\n exit 1\nfi\n\necho $0\nexit 111\n\nWhen we run the script, we\u2019ll see something like this:\n$ myscript oops\n\/home\/shs\/bin\/oops\n\nWe see the full name of the file being verified.\nWhen we check the exit code, however, all we\u2019ll see this:\n$ echo $?\n111\n\nWrap-Up\nThe true and false commands have limited functionality, but they can be helpful when you need some control over exit codes or you want to run a command until you\u2019re ready to kill it.