Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 //... ?

share|improve this question

migrated from stackoverflow.com Apr 29 '11 at 8:50

This question came from our site for professional and enthusiast programmers.

    
That doesn’t happen on Solaris, Darwin, or OpenBSD. –  tchrist Apr 29 '11 at 3:34
    
@tchrist: This behavior is specific to Bourne shell; you are probably using zsh or the like. –  grawity Apr 29 '11 at 13:14
    
@grawity: No no no. It is a namei() thing, not a shell thing!! It happens on Linux, just not on the others. –  tchrist Apr 29 '11 at 13:16
    
@thchrist: 1) What do you mean by "namei()"? If you are referring to a syscall or library function, then I haven't yet heard of such. There's only a /usr/bin/namei, but it is not related. 2) It is a shell thing, since it only happens with sh and bash, but not with zsh/csh/tcsh, nor with the chdir() syscall. 3) It happens on "the others" - I just tested NetBSD 5.0 and FreeBSD 6.3, and cd //bin shows exactly the same behavior. –  grawity Apr 29 '11 at 13:28
    
I hate to nag, but it's called a ‘slash’. There's the ‘slash’ and the ‘backslash’. The ‘forward slash’ (sic) seems to be a recent construct, and I mostly hear it from people on TV. –  Alexios Feb 21 '12 at 9:05

4 Answers 4

up vote 27 down vote accepted

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.

share|improve this answer

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.)

share|improve this answer
1  
I remember that double slash to allow remote file system access in an early system V implementation (Utek V & DFS). The syntax was //servername/path. Just like Solaris automounter would use now /net/servername/path. –  jlliagre Apr 29 '11 at 9:39
2  
    
@Gilles, thanks! :) –  sarnold Jul 10 '11 at 23:58
    
@jlliagre: I remember that from AT&T RFS. (Remote File System) Was in System V. –  janm Nov 8 '11 at 11:52

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.

share|improve this answer
    
And zsh acts like csh, but does the trimming by itself. –  Stéphane Gimenez Oct 7 '11 at 17:08

All the trailing multiple slashes are ignored and treated as single slash.

Even at the start also more than 2 slashes are ignored and treated as single slash only.

The double slash (//) at the start also means root / directory but it still shows your CWD as // when you run pwd.

Following command verify that // is also root dir same as /

$~> cd /
$/> pwd
/
$/> stat -c "%i" .
2
$/> cd //
$//> pwd
//
$//> stat -c "%i" .
2

You can see inode number is 2 in both cases hence both are same directories.

share|improve this answer
    
Trailing slashes are not ignored, they are the equivalent of "/path/." Try "cat /etc/passwd/" to see. –  janm Nov 8 '11 at 11:51
1  
Please read my answer again. I didn't say trailing slashes are ignored but instead said trailing multiple slashes are ignored. Which means /etc/passwd/ is same as /etc/passwd// –  anubhava Nov 8 '11 at 14:27
    
Ok, I see. Of course the collapsing of slashes isn't limited to the beginning or the end; /path//to//file is equivalent to /path/to/file. –  janm Nov 8 '11 at 21:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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