在命令替换中使用 find(1) 并用空格引用文件名
我想在命令替换中使用 find ,其中返回的文件名包含空格。我需要什么选项才能正确引用文件名?我尝试了 -print0
,但它在 shell 本身中不起作用。
例如:
command $(find . -type f) some other params
我也尝试过 -exec echo "{}" \;,但这也没有帮助。
如果我使用 set -x 来显示 shell 扩展和实际执行的命令,我会得到:
$ command `find -type f -printf \"%p\"\ ` some other params
++ find -type f -printf '"%p" '
+ command '"./file_with' 'blanks"' '"./another' 'file"' some other params
单引号来自哪里以及为什么它们应用于每个“单词”?
I'd like to use find inside a command substitution, where the returned filenames contain whitespace. What option do I need so it correctly quotes the filenames? I tried -print0
, but it will not work in the shell itself.
example:
command $(find . -type f) some other params
I also tried with -exec echo "{}" \;
, but that was of no help either.
If I use set -x
to display shell expansion and the actual command which is executed I get:
$ command `find -type f -printf \"%p\"\ ` some other params
++ find -type f -printf '"%p" '
+ command '"./file_with' 'blanks"' '"./another' 'file"' some other params
Where are the single quotation marks coming from and why are they applied to each "word"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 查找结果放入数组,然后运行
命令“${array[@]}”一些其他参数
。Put the find result in an array, and run
command "${array[@]}" some other params
.也许
printf
操作更适合包含在替换中(不过仅限 GNUfind
):%P
占位符是文件名减去find
的参数,因此在find .
以外的情况下,您可能需要%p
来代替。Maybe the
printf
action is more amenable to being contained in a substitution (GNUfind
only, though):The
%P
placeholder is the filename minus the argument tofind
, so in cases other thanfind .
, you'd probably want%p
instead.在这里工作(Ubuntu 10.04 默认 gterm 与 bash)
刚刚尝试了
所有预期的工作
works here (Ubuntu 10.04 default gterm with bash)
Just tried
all work as expected