Closed Captioning Closed captioning available on our YouTube channel

How to use the true and false commands: 2-Minute Linux Tips

Network World | Jan 21, 2022

In this Linux tip, learn how to use the true and false commands. What they do is very simple. The true command generates an exit code of 0 (no problems) and the false command generates a value of 1.

Copyright © 2022 IDG Communications, Inc.

Similar
Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at the true and false commands. What they do is very simple. The true command generates an exit code of 0 (no problems) and the false command generates a value of 1. Here's an example:
The $? represents the exit code.
How the true and false commands are used, on the other hand, provides some useful advantages. For one, you can start a loop using a while true command.
This loop will run forever or until you hit ^c. A while false loop would exit instantly.
You can also use true to ensure that a command that fails doesn't result in an exit code that indicates an error by piping the command's output to true. For example:
The error encountered is displayed, but we still get a 0 (no error) exit code. If you wanted a potential error to NOT stop an otherwise infinite loop, this would do the trick!
That’s your Linux tip for the true and false commands.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.
Popular
Featured videos from IDG.tv