44

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.

5
  • 4
    Workaround idea: create a symlink? Feb 2, 2013 at 11:41
  • 1
    @KarolyHorvath: Yeah, or rename the directory. But I think that the OP is not using that, he is trying to emulate the algorithm of the shell to parse the PATH and just want to know if there is a escape rule to implement it himself.
    – rodrigo
    Feb 2, 2013 at 12:03
  • Shell out to which?
    – phs
    Jan 8, 2014 at 1:12
  • 1
    You can use a directory with a : in the name to force people to type the pathname of the command as a full pathname. This could even be a legitimate ploy to make sure that sensitive commands are not booby-trapped (su and sudo spring to mind); the user who wants to execute them must either do gyrations with cd or type the full pathname because the directory can't be on the path. (Of course, a symlink would get around that, so it isn't perfect protection.) Jan 8, 2014 at 1:39
  • A reasonable way of dealing with this could have been (had they chose to do it this way...), if you want a colon, you give a double-colon. In PATH, /usr/c: would become /usr/c::. Does anyone actually *need a PATH which supports empty entries? May 14, 2021 at 18:41

4 Answers 4

32

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.

2
  • Does this hold in general for other PATH-like variables? eg. PKG_CONFIG_PATH, MAN_PATH, etc.
    – olejorgenb
    Aug 6, 2016 at 11:02
  • @olejorgenb Those aren't defined by a standard, so it depends on how the application that uses them defines it.
    – Random832
    Aug 6, 2016 at 18:26
9

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.

5
  • Also, could it be that the Apple's implementation is missing something?
    – Shahbaz
    Feb 2, 2013 at 11:48
  • @Shahbaz: No, look that the header of the file. It is not an Apple re-implementation of bash, it is the GNU one - who else?. It is just hosted there because Apple OS uses bash and GPL requires it to show the source. You can find copies of this very same file in many places.
    – rodrigo
    Feb 2, 2013 at 11:53
  • Perhaps this has changed in a newer version of Bash? The code you linked is quite old. Feb 2, 2013 at 12:07
  • Since you looked into the source code I guess that you are right. I wonder why they haven't implemented an escape sequence as colon is a common character. I thought they might be some posix wide specification but so it seems not to be.
    – Gellweiler
    Feb 2, 2013 at 12:08
  • 2
    @PhilippWendler: Actually, the linked one is for reference only. I downloaded the latest one from my Archlinux - running makepkg -o. It is bash-4.2.042 and this function is just the same.
    – rodrigo
    Feb 2, 2013 at 12:23
5

You could try mounting it

mount /bin:d /bind
PATH=/bind
0
-4

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.

1
  • 5
    This describes how bash parses expressions, it has absolutely nothing to do with how the PATH environment variable is handled.
    – poizan42
    Feb 22, 2015 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.