Which directory does path `//` represent in Linux?

Solution 1:

It can be considered either.

In Linux, // means nothing – multiple consecutive slashes get collapsed to one, anywhere in the path, including the beginning. Changing directory to // puts you in /, as running readlink /proc/self/cwd would tell; likewise, /usr//local///bin is collapsed to /usr/local/bin.

However, some other Unix-like systems, for example Cygwin or the old Apollo Domain/OS, use the // prefix for network paths such as //fileserver/path/to/data. POSIX allows this as well.

For various reasons, the bash shell tracks the current directory on its own (in addition to the OS-provided tracking) and it has code in it that prevents the initial // from being collapsed, to remain compatible with such systems. The "feature" is that bash provides more intuitive tracking of current directory, for example, when cd'ing into a symlink, bash will show you the path you expect, even though the kernel thinks otherwise. The "bug" is that bash permits // even on systems that do not use it.

Solution 2:

From the POSIX pathname definition:

A pathname may optionally contain one or more trailing slashes. Multiple successive slashes are considered to be the same as one slash.

Source

And more precisely as grawity mentioned in his comment below, from the 4.11 chapter on Pathname Resolution:

A pathname that begins with two successive slashes may be interpreted in an implementation-defined manner, although more than two leading slashes shall be treated as a single slash.

Solution 3:

It a kind of feature. If you've got shell script going and use find, for example all paths are prefixed with ./ usually. Then, if you tack that onto an actual path it becomes `/my/path/./appended/path, which resolves to /my/path/appended/path. So, if I'm not mistaken, // get's interpreted as /./ and therefore /. This is the same for if you went to /home/user//, you'd wind up in /home/user/

Solution 4:

I would go for prompt displaying "bug".

Either paths "/", "//", "//////////////////////", ... have the same meaning: "/". You can add as many "/" wherever you want in an Unix path, this not change its meaning.

The "bug" is more here related to the fact that your prompt use the last valid typed in path for display, not the actual "pwd".

Funny anyway ;)

Solution 5:

Its a Feature and all multiple // will be replaced to one single /

Its useful if you have variables with paths like the example at the end. So your cd wouldn't get any error and you don't have to change the workspace variable.

MY_WORKSPACE=/home/your_username/workspace/
MY_NEW_PROJECT=$MY_WORKSPACE/my_proj/
cd $MY_NEW_PROJECT 

the complete content of the project variable is

/home/your_username/workspace//my_proj/