Standard input contains the space-separated words alpha beta gamma.
Pass them as command-line arguments to echo.
Which command sequence is appropriate?
xargs splits standard input into items and appends them as arguments to the specified command, unlike a simple stdin pipe.
Detailed explanation
printf 'alpha beta gamma' | echoIncorrect. echo does not read its standard input when no arguments are supplied.
Incorrect. echo does not read its standard input when no arguments are supplied.
echo < printf 'alpha beta gamma'Incorrect. This treats printf as a filename for input redirection.
Incorrect. This treats printf as a filename for input redirection.
printf 'alpha beta gamma' | tee echoIncorrect. tee writes to a file named echo rather than building echo arguments.
Incorrect. tee writes to a file named echo rather than building echo arguments.
printf 'alpha beta gamma' | xargs echoCorrect. xargs echo constructs an echo invocation from the input items.
Correct. xargs echo constructs an echo invocation from the input items.
Try it yourself
An example you can run in a temporary verification environment.
printf 'alpha beta gamma' | xargs printf '<%s>\n'Expected result
<alpha>
<beta>
<gamma>Key points
- xargs converts stdin to argv
- A normal pipe connects stdin to stdin
- Quoted input needs care
Notes
- Environment: GNU findutils xargs / 標準入力
- 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.