Do not run echo RUN at all when the input is empty.
When items exist, run echo RUN with each item. Use GNU xargs.
Which option is appropriate?
GNU xargs -r (--no-run-if-empty) suppresses the command invocation when no input items are read, avoiding an unwanted side effect.
Detailed explanation
xargs echo RUNIncorrect. Without -r, GNU xargs may run echo RUN once even with no input.
Incorrect. Without -r, GNU xargs may run echo RUN once even with no input.
xargs -n 1 echo RUNIncorrect. -n 1 controls arguments per invocation, not empty-input behavior.
Incorrect. -n 1 controls arguments per invocation, not empty-input behavior.
xargs -P 1 echo RUNIncorrect. -P 1 controls parallelism, not whether an empty invocation occurs.
Incorrect. -P 1 controls parallelism, not whether an empty invocation occurs.
xargs -r echo RUNCorrect. -r prevents the command from running when input is empty.
Correct. -r prevents the command from running when input is empty.
Try it yourself
An example you can run in a temporary verification environment.
printf '' | xargs -r echo RUN; printf DONEExpected result
DONEKey points
- -r skips an empty invocation
- GNU xargs normally invokes once by default
- Avoiding unnecessary side effects
Notes
- Environment: GNU findutils 4.x
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.