What is /bin/true?

LinuxCommand

Linux Problem Overview


On a Linux system, what is /bin/true? What is it used for?

Linux Solutions


Solution 1 - Linux

/bin/true is a command that returns 0 (a truth value in the shell).

Its purpose is to use in places in a shell script where you would normally use a literal such as "true" in a programming language, but where the shell will only take a command to run.

/bin/false is the opposite that returns non-zero (a false value in the shell).

Solution 2 - Linux

From the man page:

true - do nothing, successfully

true returns a status 0.

Solution 3 - Linux

Note, it's not just silly or visually nice. It helps for example to exit a program without activating the end handlers which might mess up when doing multi threading or forked programs. Like in perl:

#!/usr/bin/env perl

exec "/bin/true";

END {
  print "This wont get printed .. would have if I just 'exit' or 'die'\n";
}

Solution 4 - Linux

I've seen it used to fool a system operation into thinking a command has run when it hasn't. If a command is faulty eg looping, you can replace it with a symlink to 'true' to get the master job to run. Only a good idea if the job replaced isn't essential.

Solution 5 - Linux

Simply saying its a program returning 0. Sometimes we need to get this value to let the script more readable. It is usually used when you need to use a command for a true value.

Solution 6 - Linux

In the UNIX shells, it's used for the same purposes as boolean constants true and false in any other language.

while true; do
    something
done
flag=true
...
if $flag; then
    something
done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHulkView Question on Stackoverflow
Solution 1 - LinuxcamhView Answer on Stackoverflow
Solution 2 - Linuxuser231967View Answer on Stackoverflow
Solution 3 - Linuxuser3072567View Answer on Stackoverflow
Solution 4 - LinuxSunshineView Answer on Stackoverflow
Solution 5 - LinuxMarcus ThorntonView Answer on Stackoverflow
Solution 6 - LinuxJordan BrownView Answer on Stackoverflow