Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
See superuser.com/q/314102/91047 –  Stéphane Gimenez Feb 13 '12 at 17:16
add comment

5 Answers

up vote 28 down vote accepted

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.

share|improve this answer
    
Interestingly, zsh allows cd //, but is smart enough to just show / in the prompt string. –  new123456 Apr 6 '12 at 0:53
add comment

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.

share|improve this answer
1  
A more proper section is 4.11 Pathname Resolution (last paragraph), since the behavior in question only appears for two leading slashes. –  grawity Feb 13 '12 at 15:42
add comment

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/

share|improve this answer
add comment

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

share|improve this answer
    
Agreed -- the // in the prompt is a side effect of your prompt environment variable ($PS1) -- how is PS1 defined? –  Doug Harris Feb 13 '12 at 14:12
    
@DougHarris the $PS1 variable is defined as [\u@\h \W]\$. Any problem with setting? –  Summer_More_More_Tea Feb 16 '12 at 1:08
    
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 Feb 16 '12 at 15:11
    
@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 Feb 21 '12 at 8:47
    
Read the bash man page and search for PS1 and PROMPT_COMMAND -- use man bash at your command line or read online –  Doug Harris Feb 21 '12 at 14:39
add comment

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/

share|improve this answer
add comment

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.