1

For bash the path separator for the environment variable $PATH is ":"

So out of curiosity:

if a have a directory named $HOME/with:character

Is there any way I can add it to the search path?

Please note that I think adding : to a filename is not a very smart idea, but I wanted to know if I could add such a directory to a search path if I ever encountered one

I tried with a backslash, but this doesn't seem to work:

export PATH="$PATH:$HOME/with\\:character"

double : (::) doesn't work either

gelonida
  • 303
  • 1
  • 3
  • 16
  • Don't use colons in file or directory names. See https://superuser.com/questions/358855/what-characters-are-safe-in-cross-platform-file-names-for-linux-windows-and-os for which other characters you should avoid too. – Tetsujin Mar 19 '21 at 11:04
  • Thanks for telling me, But this is a hypothetical questions out of curiosity. I would have expected, that `:` is not even allowed in a file / directory name. I personally stick to lowercase letters, numbers and underscores whenever possible – gelonida Mar 19 '21 at 11:08
  • On nix, afaik the only strictly 'illegal' character is `/` but `:` can be misinterpreted as a path separator too, so really should be avoided. – Tetsujin Mar 19 '21 at 11:10
  • yeah. but is there any trick to add a path containing a `:` character to a search path. Is there any kind of escaping? Again I would never use this in real life. but I'm curious – gelonida Mar 19 '21 at 11:13
  • are you sure? On my Linux the `~` is expanded prior to assigning the value to `PATH` So PATH contains `/home/gelonida/with\\:character` If I wanted a `~` I had to type `export PATH=$PATH:\~/with\\:character` (I'm using bash) – gelonida Mar 19 '21 at 11:27
  • Well, for Bash I was wrong. It seems Bash handles the tilde when assigning this way (with or without `export`). `sh` on the other hand may not. – Kamil Maciorowski Mar 19 '21 at 11:33
  • sh will definitely not expand `~` – gelonida Mar 19 '21 at 11:40
  • In fact I will remove the `~` from my question. Though the question was tagged as `bash`, it might also be read by others. This is probably a good idea as it is not relevant for the rest of the question – gelonida Mar 19 '21 at 11:42
  • What happens however is that sometimes users use a `~` in languages like `python` This will fail except they use something like `os.path.expanduser("~/with:character")` – gelonida Mar 19 '21 at 11:46

1 Answers1

3

There is no way to escape the colon. This is impossible according to the POSIX standard. PATH handling is done within the execvp function in the C library where there is no provision for any kind of quoting. This is the reason why including characters not in the portable filename character set 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. It uses the strchrnul function for processing the PATH components, with no unescaping of any kind.

Your best bet is to rename the directory. If it really needs to have that name, you can create a symbolic link and add that to your $PATH:

 $ cd /path/to/add
 $ ln -s a:b a_b
 $ PATH="$PATH:/path/to/add/a_b/bin"
harrymc
  • 455,459
  • 31
  • 526
  • 924