13

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.

1

5 Answers 5

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.

3
  • Interestingly, zsh allows cd //, but is smart enough to just show / in the prompt string.
    – new123456
    Apr 6, 2012 at 0:53
  • @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. Jan 20, 2016 at 20:13
  • Many thanks, I found the description for // in the Bitsavers manual.
    – user353322
    Jan 21, 2016 at 2:27
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.

1
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/

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

5
  • Agreed -- the // in the prompt is a side effect of your prompt environment variable ($PS1) -- how is PS1 defined? Feb 13, 2012 at 14:12
  • @DougHarris the $PS1 variable is defined as [\u@\h \W]\$. Any problem with setting? Feb 16, 2012 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? Feb 16, 2012 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. Feb 21, 2012 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 Feb 21, 2012 at 14:39
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/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .