60

In unix/linux, any number of consecutive forwardslashes in a path is generally equivalent to a single forwardslash. eg.

$ cd /home/shum
$ pwd
/home/shum
$ cd /home//shum
$ pwd
/home/shum
$ cd /home///shum
$ pwd
/home/shum

Yet for some reason two forwardslashes at the beginning of an absolute path is treated specially. eg.

$ cd ////home
$ pwd
/home
$ cd ///
$ pwd
/
$ cd //
$ pwd
//
$ cd home//shum
$ pwd
//home/shum

Any other number of consecutive forwardslashes anywhere else in a patch gets truncated, but two at the beginning will remain, even if you then navigate around the filesystem relative to it.

Why is this? Is there any difference between /... and //... ?

Shum
  • 1,355

3 Answers3

58

For the most part, repeated slahes in a path are equivalent to a single slash. This behavior is mandated by POSIX and most applications follow suit. The exception is that “a pathname that begins with two successive slashes may be interpreted in an implementation-defined manner” (but ///foo is equivalent to /foo).

Most unices don't do anything special with two initial slashes. Linux, in particular, doesn't. Cygwin does: //hostname/path accesses a network drive (SMB).

What you're seeing is not, in fact, Linux doing anything special with //: it's bash's current directory tracking. Compare:

$ bash -c 'cd //; pwd'
//
$ bash -c 'cd //; /bin/pwd'
/

Bash is taking the precaution that the OS might be treating // specially and keeping it. Dash does the same. Ksh and zsh don't when they're running on Linux, I guess (I haven't checked) they have a compile-time setting.

  • Interesting; when I do bash -c 'cd //; /bin/pwd', I get //.  Cygwin (8.23-4) with bash version 4.1.17(9)-release and pwd (GNU coreutils) 8.23.  Even more surprising, I still get // if I do /bin/pwd -P. – G-Man Says 'Reinstate Monica' Oct 29 '15 at 20:49
  • For the record, fish does not do this. If you run cd //; pwd, or fish -c 'cd //; pwd', you get /, and if you run the former with a default prompt, the prompt has just 1 slash. – trysis Sep 29 '16 at 15:02
  • @trysis Nor do zsh, ksh, pdksh, dash, hush, … Of the shells commonly found on Linux, bash is the only one that preserves a leading //. – Gilles 'SO- stop being evil' Sep 29 '16 at 16:15
22

From the POSIX Specification:

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.

I assume Linux bash keeps this behavior in case there's a compelling future use.

(I've always heard that Al Viro kept it in place because there is a feature from Plan9 that uses it, and he'd like to have that feature in Linux, but I'm having trouble finding that feature in my Plan9 documentation. But, since it is in bash instead, Al probably doesn't have anything to do with it.)

sarnold
  • 677
  • 3
  • 9
9

According to the POSIX definition, paths starting with a double-slash (//) "...may be interpreted in an implementation-defined manner, although more than two leading slashes shall be treated as a single slash." If you use csh, for example, it doesn't act the same way:

% bash -c 'cd //; pwd'
//
% csh -c 'cd //; pwd'
/

Bash appears to be storing the directory, and pwd is reporting the $PWD, while csh appears to be using the getcwd() function to get the actual directory.

jsbillings
  • 24,406