13

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?

1
  • The commands make little sense as the exit code of true is being ignored. The only side-effect is that stdout is redirected to nowhere in an unreliable way. Commented Aug 28, 2023 at 21:21

1 Answer 1

17

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

5
  • 5
    Makes perfect sense. :) I don't know why but reading "do nothing, unsuccessfully." makes me laugh. Commented Apr 24, 2011 at 20:23
  • 9
    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. Commented Apr 24, 2011 at 20:53
  • 3
    @cjm or die due to SIGPIPE Commented Apr 25, 2011 at 4:43
  • 6
    @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). Commented Apr 25, 2011 at 22:06
  • 3
    This answer is perfect except for the word "probably"...! Commented Dec 16, 2016 at 11:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.