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

13

3

When I want to go back to upper level directory in Linux by typing cd .., I typo-ed cd //. To my great surprise, no errors are reported. What's more the prompt becomes username@hostname://$. ls indicates that I'm now at root directory.

Is this a bug or a feature of the shell? If a feature, is // an alias of /? My shell is GNU bash, version 4.1.5(1)-release (i686-linux-gnu).

Thanks and best regards.

Summer_More_More_Tea

Posted 2012-02-13T13:43:18.997

Reputation: 893

See http://superuser.com/q/314102/91047

– Stéphane Gimenez – 2012-02-13T17:16:58.720

Answers

30

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.

user1686

Posted 2012-02-13T13:43:18.997

Reputation: 283 655

@BinaryZebra Bitsavers has a collection of old Unix manuals, including Apollo's – there should be plenty of examples there. The TUHS website should have something as well. – user1686 – 2016-01-20T20:13:48.853

Many thanks, I found the description for // in the Bitsavers manual. – None – 2016-01-21T02:27:23.710

Interestingly, zsh allows cd //, but is smart enough to just show / in the prompt string. – new123456 – 2012-04-06T00:53:15.337

9

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.

Shadok

Posted 2012-02-13T13:43:18.997

Reputation: 3 760

1

A more proper section is 4.11 Pathname Resolution (last paragraph), since the behavior in question only appears for two leading slashes.

– user1686 – 2012-02-13T15:42:17.873

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/

Chuck R

Posted 2012-02-13T13:43:18.997

Reputation: 135

1

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

Ouki

Posted 2012-02-13T13:43:18.997

Reputation: 1 017

Agreed -- the // in the prompt is a side effect of your prompt environment variable ($PS1) -- how is PS1 defined? – Doug Harris – 2012-02-13T14:12:34.830

@DougHarris the $PS1 variable is defined as [\u@\h \W]\$. Any problem with setting? – Summer_More_More_Tea – 2012-02-16T01:08:15.710

Odd. If I try that PS1, I only get one / when pwd = /. I wondered if you were doing any extra work to get the directory. Is $PROMPT_COMMAND set? – Doug Harris – 2012-02-16T15:11:14.710

@DougHarris Yes, this variable is set, whose value is echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#HOME/~}"; echo -ne "\007". Could you pls explain in a little bit detail? I'm not quite familiar with these two variables. Thanks. – Summer_More_More_Tea – 2012-02-21T08:47:21.977

Read the bash man page and search for PS1 and PROMPT_COMMAND -- use man bash at your command line or read online

– Doug Harris – 2012-02-21T14:39:16.333

1

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/

phschoen

Posted 2012-02-13T13:43:18.997

Reputation: 251