Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Jun 22 '10 at 5:59

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

up vote 8 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'.

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

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

share|improve this answer

You must log in to answer this question.

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