The situation I have in mind has the following structure:

% some_command | [PRODUCED OUTPUT] || echo 'no output' >&2

Here [PRODUCED OUTPUT] stands for some as-yet-unspecified testing command, whose value should be true (i.e. "success") iff some_command produces any output at all.

Of course, some_command stands for an arbitrarily complex pipeline, and likewise, echo 'no output' >&2 stands for some arbitrary action to perform in case some_command produces no output.

Is there a standard test to do the job represented above by [PRODUCED OUTPUT]? grep -qm1 '.' comes close but also reports false on an input consisting of empty lines.

Unlike Check if pipe is empty and run a command on the data if it isn't I just want to discard the input if it's present, I don't need to preserve it.

  • 2
    What's hacky about | grep -q .? O.o – muru Apr 13 '16 at 14:47
  • @muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do... – kjo Apr 13 '16 at 14:59
  • @muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion. – kjo Apr 13 '16 at 15:00
  • Isn't testing whether input has something the intended purpose of grep? – muru Apr 13 '16 at 15:01
  • 1
    ifne (moreutils): runs a given command if and only if the standard input is not empty – HappyFace Sep 20 at 18:51

How about using read?

$ cat /dev/null | read pointless || echo no output
no output
$ echo something | read pointless || echo no output
$ printf "\n" | read pointless || echo no output
$ printf " \n" | read pointless || echo no output
$ false | read pointless || echo no output
no output

According to the Open Group definition:


The following exit values shall be returned:


Successful completion.


End-of-file was detected or an error occurred.

| improve this answer | |
  • This also report no output when read false. – cuonglm Apr 13 '16 at 14:53
  • How do you trigger that? – Jeff Schaller Apr 13 '16 at 14:56
  • @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of? – Jeff Schaller Apr 14 '16 at 15:13
  • Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output. – cuonglm Apr 14 '16 at 15:19
  • Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations. – Jeff Schaller Apr 14 '16 at 15:43

For me worked great | read -n 1.

$ echo 'faw' | read -n 1 || echo 'no output'
$ echo | read -n 1 || echo 'no output'
$ printf '' | read -n 1 || echo 'no output'
no output
$ false | read -n 1 || echo 'no output'
no output
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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