7

In bash if you cd to // your pwd will be //, but if you cd to / or ///+ your pwd will be /. Is there a reason for this or is it just a weird bug?

I have tried this in osx and ubuntu.

8

From the Bash FAQ:

E10) Why does `cd //' leave $PWD as `//'?

POSIX.2, in its description of `cd', says that three or more leading slashes may be replaced with a single slash when canonicalizing the current working directory.

This is, I presume, for historical compatibility. Certain versions of Unix, and early network file systems, used paths of the form //hostname/path to access `path' on server `hostname'.

  • cool I though it was a bug... :) – Johan Jun 22 '10 at 6:11
  • 2
    Note Windows still uses // for network paths, so this may be a SMB compatibility thing too. – Fake Name Jun 22 '10 at 6:53
  • 5
    Technically, Windows uses \\ for UNC paths. – ThatGraemeGuy Jun 22 '10 at 7:13
0

Though the answer was correct on the data it gave, it didn't quite answer the question asked.

The shell is normalizing the path. It does:

  • change any part1/part2/.. components to part1/
  • change any //+ components to /

You can verify this with strace, which will see what is actually passed into the syscall

strace -o /tmp/strace.out bash -c "cd ///tmp"
grep chdir /tmp/strace.out
# will give chdir("/tmp")

strace -o /tmp/strace.out bash -c "cd ///tmp/../etc"
grep chdir /tmp/strace.out
# will give chdir("/etc")

The shell does the path normalization before it even tells the system to change the dir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.