How to get xargs not to eat double spaces I'm trying to process some files with double spaces in the name. It seems like xargs is removing spaces. Example

$ echo "foo  bar" | xargs -I {} echo "{}"
foo bar


Notice there's only 1 space between foo and bar but 2 were passed in. The example above works in Ubuntu 16.04 bash

Note I'm actually trying to process a list of files as in

cat list-of-files | xargs -I {} echo "{}"


-O doesn't work in this case as xargs sees the entire file as one filename.  Tried -E '\n' but that still ate the extra space
Here are 3 possibilities that may work for you:

1. BSD xargs

This is built in to MacOS, but will require you to quote the filenames in your list-of-files

$ more list-of-files
'file1'
'file2'
'file 3'
'file  4'


note: I added the -t option in order to show the generated command before it executes.

$ cat list-of-files | xargs -t -I {} echo "{}"
echo file1
file1
echo file2
file2
echo file 3
file 3
echo file  4
file  4


2. GNU xargs

GNU xargs is not installed on MacOS by default, but can be installed via homebrew (in the findutils package).

brew install findutils

Using GNU xargs via homebrew would not require you to quote the filenames, and would give the same result as using BSD xargs, and quoting the filenames (option 1, above).

3. find (instead of list-of-files)

Rather than using a file for your input, use the find command. Something like the following should work:

$ ls -laF file*
-rw-r--r--  1 sfederman  staff  0 Jun  7 22:53 file  4
-rw-r--r--  1 sfederman  staff  0 Jun  7 22:53 file 3
-rw-r--r--  1 sfederman  staff  0 Jun  7 22:53 file1
-rw-r--r--  1 sfederman  staff  0 Jun  7 22:53 file2

$ find . -name "file*" | xargs -0 -I {} echo "{}"
./file  4
./file 3
./file1
./file2


Or, avoiding xargs completely:

$ find . -name "file*" -exec echo "{}" \;
./file  4
./file1
./file2
./file 3