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.

link|improve this question
feedback

migrated from stackoverflow.com Jun 22 '10 at 5:59

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 7 down vote accepted

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'.

link|improve this answer
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. – Graeme Donaldson Jun 22 '10 at 7:13
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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