Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that may in some circumstances have nothing on stdout. At the end of the script, I pass the lines I have to another command using xargs. My problem is, when there is nothing on stdout, I get the typical "Usage: program arg1 arg2" error message.

How can I avoid this?

share|improve this question
Move your "EDIT" in an answer and accept your own answer. This way your discoveries will be useful for other persons. meta.unix.stackexchange.com/questions/809/… – andcoz Jan 18 '13 at 21:34
Didn't have enough points at the time. Will do it now. – Richard Jan 19 '13 at 2:01
up vote 10 down vote accepted

You need an -r or --no-run-if-empty options.

Keep in mind that this particular behavior is hard to make cross-platform. BSD versions of xargs run with -r by default. GNU version needs it. FreeBSD version of xargs ignores -r flag for compatibility with GNU. Mac OS X version does not even accept the flag and throws an error illegal option.

You might then choose to use an OS detection based on $OSTYPE to write a cross-platform script. Even better, try to detect the behavior of xargs itself. Run it with -r and if that fails (status code > 0), run it without -r.

share|improve this answer

A lesson in why you should read the man pages…

The -r or --no-run-if-empty flags are my friend.

share|improve this answer

Your Answer


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.