如何处理包含有问题(= shell 元和空格)字符的路径名?
我经常会遇到名称奇怪的文件,例如 /foo/bar * baka/waka xoxotinh(|).mp3
。为了正确引用文件,以任意语言转义这个东西的正确方法是什么?
Often I have files with weird names like /foo/bar * baka/waka xoxotinh(|).mp3
. What is the correct way to escape this thing in an arbitrary language in order to correctly reference the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
\
: 转义每个保留字符,或者将其放在引号中,如下所示:
Escape each reserved character with a
\
:or put it in quotes like this:
您可以通过在
''
中包含整个路径或通过\
转义单个“奇怪”字符来转义它。这并不总是一件容易的任务,因此如果您直接从 bash 提示符执行此操作,则只需使用
TAB
来完成路径 - bash 将为您转义“奇怪”的字符。以下是 bash 转义的方法:
注意:我通过以下方式创建了路径:
You can escape it by including the whole path in
''
or by escaping individual "weird" chars by\
.This is not always an easy task, so if you do it right from bash prompt, then just use
TAB
to complete the path - bash will escape "weird" chars for you.Here's how bash escaped it:
Note: I've created the path by:
在任意语言中,它完全取决于语言及其引用约定。在我熟悉的大多数语言中,您的示例文件名并不是特别有问题,除了在 shell 脚本中;在 sh 和衍生产品中,对此类事情使用单引号。
In an arbitrary language, it depends entirely on the language, and its quoting conventions. In most languages I am familiar with, your example file name is not particularly problematic, except in shell script; in sh and derivatives, use single quotes for this sort of thing.
您需要在引号之间添加文件名,例如
"asd * sdd||ss.mp3"
或转义字符。转义意味着您需要在每个无效字符前添加\
。在 php 中你有 http://www.php.net/manual/en/function例如.escapeshellarg.php
You need to either add the filename between quotes like
"asd * sdd||ss.mp3"
, or escape characters. Escaping means you need to add a\
before each invalid character.in php you have http://www.php.net/manual/en/function.escapeshellarg.php for example
在 BASH 中,您可以使用双引号来删除
*
和?
的通配含义。反引号稍微麻烦一些。你必须把它们加倍。事实上,您可以简单地在 Bash 中的任何非字母数字字符前面添加一个反
勾号斜杠,并且非常安全。这在 Linux 上的 Bash 中非常有效:In BASH, you can use double quotes to remove the globbing meaning of
*
and?
. Backticks are a bit more troublesome. You'll have to double up those.In fact, you can simply add a back
tickslash in front of any non-alpha numeric character in Bash and be quite safe. This worked quite nicely in Bash on Linux:除了用反斜杠引用每个 shell 元字符和空格之外,没有通用解决方案。如果您用“单引号”引用,则会出现包含单引号的名称的问题。如果您用“双引号”引用,则包含双引号和美元符号的名称会出现问题。当然,也有包含两者的名称...但是您可以使用不同的引用样式引用名称的部分内容:
请注意第二个双引号如何结束第一个部分,下一个字符是引用其余部分的单引号。那里!引用了包含两个引号的名称!一旦 shell 执行了引号删除(始终是最后),
ls
将被视为单个参数。
There is no general solution other than to quote each shell meta character and whitespace with a backslash. If you quote with 'single quotes' there's the problem with names containing a single quote. If you quote with "double quotes" there's the problem with names containing double quotes and dollar signs. And of course there are names containing both... But you can quote parts of names with different quoting styles:
Note how the second double quote ends the first part, and the next character is the single quote that quotes the remainder. There! Quoted a name containing both quotes! Once the shell has performed quote removal (always last),
ls
will seeas a single argument.