Reputation: 342
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
Reputation: 295403
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