Why does xargs strip quotes from input?

Why does xargs strip quotes from input text?

Here is a simplified example:

echo "/Place/='http://www.google.com'" | xargs echo

outputs

/Place/=http://www.google.com

Solution 1:

From the xargs manual:

If you want an input argument to contain blanks or horizontal tabs, enclose it in double quotes or apostrophes. If the argument contains a double quote character ("), you must enclose the argument in apostrophes. Conversely, if the argument contains an apostrophe ('), you must enclose the argument in double quotes. You can also put a backslash (\) in front of a character to tell xargs to ignore any special meaning the character may have (for example, white space characters, or quotes).

This means you can escape quotes if the quotes are quoted themselves:

$ echo "/Place/=\'http://www.google.com\'" | xargs echo
/Place/='http://www.google.com'

 

Solution 2:

if you want xargs to ignore quotes one of the good soultion can be the use of xargs flag xargs -0

Directly from Man page OPTIONS

OPTIONS -0, –null

Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.

I’ve checked on a GNU system that setting the delimiter to a specific value (like a newline) with -doption (and not just -0) would also cause xargs not to treat the quotes etc specially.

Echo to all files matching pattern

I have some files in some directory.
For example:

/path/file1.out
/path/file2.out
/path/file3.out
/path/file4.out
/path/file5.out

Is it possible to write something to all of these files using echo with a single command?
I would expect something like

echo "asd" > /path/file*.out

but it is not recognizing the glob.

Solution:

It is not possible to redirect to more files, you need to use a loop:

shopt -s nullglob
for file in /path/file*.out; do
   echo "foo" > "$file"
done

Or you can use tee that will output to stdout at the same time:

shopt -s nullglob
echo "foo" | tee /path/file*.out

Note that shopt -s nullglob is important so that you don’t create an undesired file if there are no files matching your pattern.