user8328365
user8328365

Reputation: 342

Why does piping a string with literal quotes to xargs make them disappear?

I am writing bash scripts which contains chained printf pipeline. (imaging constructing JSON objects)

My usage is something like this:
printf \"abc\" | xargs printf
The result is abc without quotes. What I need to do if I want to preserve the quotes in the second printf output?

Upvotes: 0

Views: 357

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

This is xargs behaving as-documented; it implements shell-like (but not strictly shell-compatible) parsing and quote removal.

Both GNU and BSD platforms have extensions beyond the POSIX baseline that let you disable this.

On systems with either GNU or BSD extensions, use -0 and delimit your input with NULs:

printf '%s\0' \"abc\" | xargs -0 printf

With GNU xargs, you can use -d to specify a delimiter to use -- a context in which a newline is valid:

printf '%s\n' \"abc\" | xargs -d $'\n' printf

Upvotes: 1

Related Questions