Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
[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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
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. –  Stéphane Chazelas Jan 20 at 15:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.