4
[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?

1
  • I'm trying to resolve this same issue. No matter how many new lines (\n), spaces, etc. you add xargs wants to concatenate all the input when there is no argument. Unless you use the -0 option. However, evne then xargs will only echo the input lines. This is annoying because you have to remember to use grep -z and what about commands that don't a similar -print0 option(?).
    – will
    Mar 5, 2020 at 1:00

3 Answers 3

6

Without 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...".

Removal of newlines?

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.

1
  • 1
    It doesn't split by newline by default, it splits by sequences of space, tab or newline where single quote, double quote or backslash can escape the separators (and each others). To split by newline, you need either the GNU specific -d $'\n' or escape every blank and quote character except newline. Jan 20, 2014 at 15:48
2

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 for xargs also uses a null character as a separator. If that program is GNU find for example, the -print0 option does this for you.

1

Yes. Without arguments, xargs replaces newlines with spaces, because it:

  1. Bundles up all the whitespace-separated input items into a single space-separated list of up to ARG_MAX items; and
  2. Passes that list to /bin/echo; which
  3. Prints the list of items passed to it, on a single line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .