I can reproduce this issue on cygwin bash and oh-my-zsh with wsl.exe. My intent is to run these two lines on the command line:
npm rm plugin-alias
npm i -D plugin-alias
Here is how I'm trying to do that:
echo -ne "rm\0i -D\0" | xargs -t -0 -IREPLACE npm REPLACE plugin-alias
The rm
runs fine. -t
says it is running as npm rm plugin-alias
. But i -D
errors:
npm 'i -D' plugin-alias
Unknown command: "i -D"
Is there a way to get xargs to pass i -D
as multiple arguments so it runs npm i -D plugin-alias
instead?
I've tried changing the delimiters to ,
and \n
, but it doesn't seem to make a difference.
If I have to involve sed
to remove the quotes, so be it, but I'm hoping xargs
can solve the problem it appears to have introduced.
UPDATE: @muru provided a helpful comment:
@muru: there're no actual quotes involved
me: then why do I get the error message, Unknown command: "i -D"?
@muru: Because xargs is providing i -D as a single argument to npm.
Now that I understand, I can edit my title to be more accurate. For better searchability, I want to say the original title was, "Why does xargs quote items with spaces and how do you prevent it?"
xargs
is providingi -D
as a single argument tonpm
.xargs
is providingi -D
as a single argument tonpm
. You say you want to runnpm i plugin-alias
, but then you try to runnpm i -D plugin-alias
. Those are obviously not the same thing.i -D
has a space in it, not a NUL, so as far as xargs is concerned, it's just one argument (so it runs npm with that argument as you told it to, and npm is printing an error msg about the arg it doesn't understand,i -D
, and quoting it to make it obvious that that's the problematic arg). BTW, even if you changed it toi\0-D
, it still wouldn't do what you want - it would run npm once withi
and again with-D
.