Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 333
Gellweiler Avatar asked Feb 02 '13 11:02

Gellweiler


People also ask

How do I escape the colon in Linux?

In bash ; escape with '\'.

How do I escape a character in Unix?

z/OS UNIX System Services User's GuideAll escape sequences are made from a backslash character ( \ ) followed by one to three other characters. Escape sequences are used inside strings, not just those for printf, to represent special characters. In particular, the \n escape sequence represents the newline character.

How do you escape a character in Linux terminal?

Escape characters. Escape characters are used to remove the special meaning from a single character. A non-quoted backslash, \, is used as an escape character in Bash. It preserves the literal value of the next character that follows, with the exception of newline.

How do you escape a variable in bash?

Escape characters: Bash escape character is defined by non-quoted backslash (\). It preserves the literal value of the character followed by this symbol. Normally, $ symbol is used in bash to represent any defined variable.


2 Answers

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.

like image 159
Random832 Avatar answered Sep 23 '22 19:09

Random832


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.

like image 34
rodrigo Avatar answered Sep 25 '22 19:09

rodrigo