How to escape colon (:) in $PATH on UNIX?

后端 未结 4 1172
予麋鹿
予麋鹿 2020-12-08 09:21

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 te

相关标签:
4条回答
  • 2020-12-08 10:11

    According to http://tldp.org/LDP/abs/html/special-chars.html single quotes should preserve all special characters, so without trying it, I would think that '/bin:d' would work (with)in $PATH.

    0 讨论(0)
  • 2020-12-08 10:12

    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.

    From SUSv7:

    Since <colon> is a separator in this context, directory names that might be used in PATH should not include a <colon> character.

    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.

    0 讨论(0)
  • 2020-12-08 10:19

    You could try mounting it

    mount /bin:d /bind
    PATH=/bind
    
    0 讨论(0)
  • 2020-12-08 10:21

    Looking at the function extract_colon_unit it seems to me that this is impossible. The : is unconditionally and inescapably used as the path separator.

    Well, this is valid at least for bash. Other shells may vary.

    0 讨论(0)
提交回复
热议问题