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.
Best Answer
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 runningreadlink /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, whencd
'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.