Playing with xargs
We use pipes to redirect stdout
(standard output) of a command to stdin
(standard input) of another command. For example:
cat foo.txt | grep "test"
Some of the commands accept data as command-line arguments rather than a data stream through stdin
(standard input). In that case, we cannot use pipes to supply data through command-line arguments.
We should try alternate methods. xargs
is a command that is very helpful in handling standard input data to the command-line argument conversions. It can manipulate stdin
and convert to command-line arguments for the specified command. Also, xargs
can convert any one-line or multiple-line text inputs into other formats, such as multiple lines (specified number of columns) or a single line ...
Get Linux Shell Scripting Cookbook - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.