如何在 UNIX 上的 $PATH 中转义冒号 (:)? [英] How to escape colon (:) in $PATH on UNIX?
问题描述
我需要解析应用程序中的 $PATH
环境变量.所以我想知道在 $PATH
中哪些转义字符是有效的.我创建了一个名为 /bin:d
的测试目录并创建了一个名为有趣
在里面.如果我用绝对路径调用它,它就会运行.我就是不知道如何在 $PATH
中转义 :
我试图转义冒号与 并将其包装成单
'
和双 "
引号.但是总是当我运行 which 有趣
时它找不到它.我正在运行 CentOS 6.
I need to parse the $PATH
environment variable in my application.
So I was wondering what escape characters would be valid in $PATH
.
I created a test directory called /bin:d
and created a test script called
funny
inside it. It runs if I call it with an absolute path.
I just can't figure out how to escape :
in $PATH
I tried escaping the
colon with and wrapping it into single
'
and double "
quotes.
But always when I run which funny
it can't find it.
I'm running CentOS 6.
推荐答案
根据 POSIX 标准,这是不可能的.这不是特定 shell 的函数,PATH 处理是在 C 库中的 execvp 函数中完成的.不提供任何形式的引用.
This is impossible according to the POSIX standard. This is not a function of a specific shell, PATH handling is done within the execvp function in the C library. There is no provision for any kind of quoting.
这就是为什么强烈建议不要包含某些字符(任何不在可移植文件名字符集"中的字符 - 冒号作为示例特别指出.)的原因.
This is the reason why including certain characters (anything not in the "portable filename character set" - colon is specifically called out as an example.) is strongly recommended against.
来自 SUSv7:
由于
是此上下文中的分隔符,因此可能在 PATH 中使用的目录名称不应包含
字符.
Since
<colon>
is a separator in this context, directory names that might be used in PATH should not include a<colon>
character.
另见GLIBC execvp 来源.我们可以看到它使用 strchrnul
和 memcpy
函数来处理 PATH 组件,绝对没有跳过或取消任何类型的转义字符的规定.
See also source of GLIBC execvp. We can see it uses the strchrnul
and memcpy
functions for processing the PATH components, with absolutely no provision for skipping over or unescaping any kind of escape character.
这篇关于如何在 UNIX 上的 $PATH 中转义冒号 (:)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!