[user@notebook ~] echo -e '1\n2\n3\n4'
1
2
3
4
[user@notebook ~] echo -e '1\n2\n3\n4' | xargs
1 2 3 4
[user@notebook ~]
My question: So xargs removes the newlines if it's used without parameters?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityWithout arguments xargs
defaults to echoing out the parameters that are passed to it.
from the man page
This manual page documents the GNU version of xargs. xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is
/bin/echo
) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
Notice the bit about "...default is /bin/echo
...".
That's kind of the purpose of xargs
. It takes a list of arguments, often split by spaces & newlines (can be split by other delimiters), and repackages them as a single argument, optimizing the arguments so that they fit within a ARG_MAX's worth of space.
-d $'\n'
or escape every blank and quote character except newline.
Jan 20, 2014 at 15:48
quoting the manpage:
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is
/bin/echo
) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by
xargs
. In these situations it is better to use the-0
option, which prevents such problems. When using this option you will need to ensure that the program which produces the input forxargs
also uses a null character as a separator. If that program is GNUfind
for example, the-print0
option does this for you.
Yes. Without arguments, xargs
replaces newlines with spaces, because it:
ARG_MAX
items; and/bin/echo
; which
\n
), spaces, etc. you addxargs
wants to concatenate all the input when there is no argument. Unless you use the-0
option. However, evne thenxargs
will onlyecho
the input lines. This is annoying because you have to remember to usegrep -z
and what about commands that don't a similar-print0
option(?).