如何在UNIX上的$ PATH中转义冒号(:)? [英] How to escape colon (:) in $PATH on UNIX?
问题描述
我需要在我的应用程序中解析 $ PATH
环境变量。
所以我想知道什么转义字符在 $ PATH
中有效。
我创建了一个名为 / bin:d
的测试目录,并创建了一个名为
的测试脚本
里面如果我用一个绝对路径称它,它运行。
我不知道如何逃避:
在 $ PATH
我试图逃避
冒号与 \
并将其包装成单个'
和双重$ code code code code code code code code code code code code code code code $ c $ p>
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处理是在execvp函数内完成的在C库中,没有任何类型的引用条款。
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中使用的目录名不应包含< colon>
字符。
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屋!