If on a GNU system, look into the --delimiter
argument to xargs
. Since your input is separated by newlines, you needn't rely on quoting at all, or use any additional tools:
cat files.txt | xargs --delimiter=\\n printf "<%s>"
Where files.txt
contains your input:
line number 1
line number 2
line number 3
Gives the output:
<line number 1><line number 2><line number 3>
NB: You do not need to store your input in a file and use cat
; you may of course pipe directly from another program. I used cat
in my example due to its ubiquity and utility in creating self-contained examples such as this.
Even better (and recommended by the xargs
manual) is to have your output program separate the entries by the NUL (\0
) instead of a newline, and use the --null
option instead of --delimiter
, but as long as your input strings will never themselves contain newlines, you're OK with the above approach.
xargs -0
, which delimits the values with theNUL
character. The input program will need to know how to do that, though (e.g.find -print0
and so forth). – thrig yesterdayxargs -d '\n'
might be even more useful here, if the OP is using GNU xargs. (Neither the-0
nor the-d
option is standardized by POSIX, so they're both potentially non-portable. BSD xargs does seem to support-0
but not-d
, though.) – Ilmari Karonen 21 hours ago