I'm trying to invoke a script with a list of filenames collected by
find. Nothing special, just someting like this:
$ myscript `find . -name something.txt`
The problem is that some of the pathnames contain spaces, so they get broken up into two invalid names on argument expansion. Normally I would surround the names with quotes, but here they're inserted by the backquote expansion. I've tried filtering the output of
find and surrounding each filename with quotes, but by the time bash sees them, it's too late to strip them and they are treated as part of the filename:
$ myscript `find . -name something.txt | sed 's/.*/"&"/'` No such file or directory: '"./somedir/something.txt"'
Yes, that's the rules for how the command line is processed, but how do I get around it?
This is embarrassing but I'm failing to come up with the right approach. I finally figured out how to do it with
xargs -0 -n 10000... but it's such an ugly hack that I still want to ask: How do I quote the results of backquote expansion, or achieve the same effect in another way?
Edit: I was confused about the fact that
xargs does collect all arguments into a single argument list, unless it's told otherwise or system limits might be exceeded. Thanks to everyone for setting me straight! Others, keep this in mind as you read the accepted answer because it's not pointed out very directly.
I've accepted the answer, but my question remains: Isn't there some way to protect spaces in backtick (or
$(...)) expansion? (Note that the accepted solution is a non-bash answer).