PDA

View Full Version : Where is '//'?



jeffcobb
07-12-2010, 06:09 PM
This is a dumb question that I thought of when working whilst tired and more than once when meaning to 'cd ..' (go up one directory) did a 'cd //'. Where exactly does that leave you? pwd just gives the obvious and frankly useless:


jeff@jeffdeb://$ pwd
//

Now doing an ls shows me to be in the root (not /root but /) but there has GOT to be some differences between /root and //. Anyone with a clue?

brewbuck
07-12-2010, 06:30 PM
This is a dumb question that I thought of when working whilst tired and more than once when meaning to 'cd ..' (go up one directory) did a 'cd //'. Where exactly does that leave you? pwd just gives the obvious and frankly useless:


jeff@jeffdeb://$ pwd
//

Now doing an ls shows me to be in the root (not /root but /) but there has GOT to be some differences between /root and //. Anyone with a clue?

You're seeing the shell's (not quite so intelligent) caching of the current directory. If you actually called getcwd(), the system call, it would return "/".

UNIX path parsing treats multiple slashes as a single slash. You should be in the root dir.

EDIT: Hmm. Maybe there's more to it. When I "cd //" I see the same thing you are seeing. But "cd ///" takes me to plain old "/". Not sure now!

Here's what Python says:



scott@VM-Kubuntu-1:~$ cd //
scott@VM-Kubuntu-1://$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.getcwd()
/


Python uses the real getcwd() call, so I think it really is just "/" and you're seeing some shell funniness.

kermit
07-12-2010, 06:43 PM
It looks like you are using Bash, and it does not quite know what to make of your cd. In zsh, you can do cd //, and it takes you to /, as well as listing / (as opposed to //) in the shell prompt. Also, pwd gives /, and not //. It may seem a little odd, but so is (in my opinion) the output of ls -la in root, i.e.,


drwxr-xr-x 21 root root 4096 2010-07-04 01:15 .
drwxr-xr-x 21 root root 4096 2010-07-04 01:15 ..

There is no parent directory, yet it lists it. I know I read the explanation of that somewhere, and no doubt there is a very good reason for it. But at first glance, it does not seem very intuitive. Anyway, by doing // you are saying go to the directory / relative to / - As you can see, from my listing above, that directory does exist, and Bash took you there. I vote that it is a bug of some sort though.

brewbuck
07-12-2010, 08:34 PM
There is no parent directory, yet it lists it. I know I read the explanation of that somewhere, and no doubt there is a very good reason for it. But at first glance, it does not seem very intuitive. Anyway, by doing // you are saying go to the directory / relative to / - As you can see, from my listing above, that directory does exist, and Bash took you there. I vote that it is a bug of some sort though.

The parent directory of the root directory is the root directory. That is defined by POSIX, and just the way it is. The idea of a special directory which has no parent, seems even more confusing than a directory which is its own parent. Programs which used ".." would then have to continually check whether a given directory is the root directory in order to avoid errors.

Really, I think the shell is caching the current directory and somehow "//" makes it do something weird. Try tcsh instead:



scott@VM-Kubuntu-1:~$ tcsh
VM-Kubuntu-1:~> cd //
VM-Kubuntu-1:/> pwd
/


It's clearly just weirdness of the shell. I hesitate to call it a bug, but if it is, it's a bug in bash.

jeffcobb
07-13-2010, 10:56 PM
I thought it might have something to do with network shares like //mybox but it is more-likely a bug in bash. Hurts nothing, just made me curious....thanks for the braincells on this guys...