Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a makefile where I'm stopping a service before removing a file. When it couldn't stop the service, it would break on error. This is clearly unwanted so I thought I'd add || true but missed a |. Making it:

stop service foo | true
rm /etc/init/foo.conf

I'm confused as to why this is working and what is happening. Does this mean that true is an application and not merely a keyword? Are they the same? Is there a good reason to use | true?

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

true and false are coreutils (also typically shell built-ins) that just return 0 and non-0, for situations where you happen to need that behavior. From the man pages:

true - do nothing, successfully
false - do nothing, unsuccessfully

So you're piping the output from stop service foo into true, which ignores it and returns 0. Technically it works, but you should probably use || true so it's obvious what your intention was; there's really no reason to pipe output into a program that's not using it

share|improve this answer
    
Makes perfect sense. :) I don't know why but reading "do nothing, unsuccessfully." makes me laugh. –  Kit Sunde Apr 24 '11 at 20:23
5  
Another reason to avoid | true is that if the command produced enough output to fill up the pipe buffer, it would block waiting for true to read it. –  cjm Apr 24 '11 at 20:53
1  
@cjm or die due to SIGPIPE –  Andy Apr 25 '11 at 4:43
3  
@Kit: Note that foo || true and foo | true won't do the same thing: foo || true will show the output from foo, whereas foo | true will discard everything foo writes to its standard output (and foo is likely to die with SIGPIPE or block, as already indicated). –  Gilles Apr 25 '11 at 22:06
add comment

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.