28

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

Is there any way to work-around this? (xargs -0 doesn't help me)

  • 1
    xargs treats quotes and backslashes specially as part of the spec. Post what you are trying to do with xargs instead. – jw013 May 8 '12 at 13:31
  • 3
    xargs -0 works for me here... Why doesn't it help you? – derobert Apr 8 '13 at 16:24
10

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'

will work but echo /Place/=\'http://www.google.com\' | xargs echo will not.

  • 1
    Nothing against down votes but a comment on the reason would help :-) – Matteo Apr 20 '15 at 5:35
  • 3
    What if you're piping in input? If I have a script that outputs "/Place/='http://www.google.com'", how can I properly escape it? – Roger Filmyer Jul 7 '15 at 21:19
  • 1
    @RogerFilmyer your_script | parallel --shellquote | ... – Ole Tange Apr 18 '16 at 12:40
23

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 -d option (and not just -0) would also cause xargs not to treat the quotes etc specially:

-bash-4.3$ { echo "a'b'c"; echo d; } | xargs -d$'\n' echo
a'b'c d
-bash-4.3$ rpm -qf "$(which xargs)"
findutils-4.6.0.0.99.11a05-alt1.x86_64
-bash-4.3$ { echo "a'b'c"; echo d; } | xargs echo
abc d
-bash-4.3$ 
10

I found another solution in the manpage: explicitly specify delimiter to be '\n'. This turns off special handling for quotes:

--delimiter=delim, -d delim

Input items are terminated by the specified character. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf command. Multibyte characters are not supported. When processing the input, quotes and backslash are not special; every character in the input is taken literally.

So,

echo "/Place/='http://www.google.com'" | xargs -d'\n' echo

outputs

/Place/='http://www.google.com'
  • Note that it implies GNU xargs or compatible – Stéphane Chazelas Dec 5 '17 at 17:20
  • Also note that it also stops space and tab from being recognised as delimiters (probably just as well in the OP's case). – Stéphane Chazelas Dec 5 '17 at 17:22
  • Balman gave a similar solution (well actually @imz) though yours is somewhat better in that it avoids the use of the ksh93 $'...' quoting operator that is not found in every shell implementation. – Stéphane Chazelas Dec 5 '17 at 17:24
8

You could use GNU Parallel instead:

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

Then you do not have to do the quoting yourself.

Learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

4

I found another solution here https://stackoverflow.com/a/17468560/1795821 that suggests using sed to escape quotes.

For example:

sh-3.2$ echo "/Place/='http://www.google.com'" | sed "s/\'/\\\'/g" | xargs echo
/Place/='http://www.google.com'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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