81

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

codeforester
  • 39,467
  • 16
  • 112
  • 140
Hulk
  • 32,860
  • 62
  • 144
  • 215
  • /foo/bar || true (works if /bin/true) is in the path. While not labeled as such, this question does explain something that is useful in shell scripts, makefiles, etc.. thus not voting to close. – Tim Post Feb 01 '10 at 14:16
  • 1
    http://forums.thedailywtf.com/forums/t/3779.aspx – Colin Pickard Jul 22 '11 at 16:02
  • You may want to have a look at [the source code of them](https://askubuntu.com/questions/454117/why-is-bin-true-such-a-large-file-how-would-i-find-the-source-code); since I was curious. – Константин Ван Jun 07 '17 at 05:42

6 Answers6

91

/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).

camh
  • 40,988
  • 13
  • 62
  • 70
30

From the man page:

true - do nothing, successfully

true returns a status 0.
devnull
  • 118,548
  • 33
  • 236
  • 227
user231967
  • 1,935
  • 11
  • 9
  • 47
    SyaZ: You may also be amused by the description of `false(1)`: "do nothing, unsuccessfully". – camh Feb 02 '12 at 10:49
10

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";
}
user3072567
  • 101
  • 1
  • 2
2

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.

Sunshine
  • 21
  • 1
1

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.

Marcus Thornton
  • 5,955
  • 7
  • 48
  • 50
0

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
Jordan Brown
  • 374
  • 3
  • 4
  • 1
    how is that different from `cd .` ? – padjee Oct 22 '22 at 13:56
  • 1
    Well, for one thing, `cd .` can fail. `mkdir foo; cd foo; rmdir ../foo; cd .` But mostly it isn't different, nor is it different from `:`, nor any of a ton of other ways to force a command to succeed. But it's a lot clearer than any of those. – Jordan Brown Oct 23 '22 at 17:12
  • 1
    `cd .` in a deleted directory does seem to work (with a warning message) in some shells. But not all. – Jordan Brown Oct 23 '22 at 17:20