Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I just found out about /bin/true yesterday. I'm not a bash power user by any means, but I decided to check it out. I was surprised to find that it is a 22K file (Ubuntu 12.04), and not one of the smallest utilities in /bin (e.g. kill is smaller). This is really just curiosity, but is there are reason for it not to be a very small bit of code? How would I go about finding the source code for /bin/true?

share|improve this question
    
int main () { return 0; } - 26 characters - and that's all that's necessary. Compiles and strips to 6K on my machine. GNU's source is here. –  Dennis Williamson Oct 10 at 21:54

3 Answers 3

up vote 2 down vote accepted

Well, looking into the sources of the coreutils tools, true is one of the smallest. It is smaller than kill, though larger than false which is the very smallest:

$ for i in src/*c; do wc -l $i; done | sort -nr | tail -11
78 src/true.c
74 src/operand2sig.c
73 src/sync.c
37 src/prog-fprintf.c
2 src/uname-uname.c
2 src/uname-arch.c
2 src/ls-vdir.c
2 src/ls-ls.c
2 src/ls-dir.c
2 src/lbracket.c
2 src/false.c

So, true has 78 lines, but only 58 of them are actually code (the rest are blank lines and comments):

$ grep . src/true.c | awk '{ if(/\/\*/){a=0} if(a){print} if(/\*\//){a=1}}' | wc
50

And yes, a lot of that (10 lines) are dedicated to the usage function. The program is still tiny though.

Also, the reason that false is tinier is that it simply calls true:

$ cat src/false.c
#define EXIT_STATUS EXIT_FAILURE
#include "true.c"
share|improve this answer
$ dpkg -S /bin/true
coreutils: /bin/true

So the package which actually provides /bin/true file is coreutils.

Run the below command to download the source code of coreutils,

apt-get source coreutils
share|improve this answer

true is part of the GNU coreutils. Their homepage is here, and there are download links for the source there. With git, you can download the source using:

git clone git://git.sv.gnu.org/coreutils
share|improve this answer
    
Thank you! And by viewing "true.c" I can begin to answer the rest of my question. It looks like a lot of code may be devoted to supporting --version and --help? –  Steve Koch Apr 24 at 1:58
    
That, and anything that is linked in statically. I haven't examined the build files myself. –  jkt123 Apr 24 at 2:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.