Why can bash cd to //?

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.

Craig

Posted 2010-06-22T05:09:32.457

Reputation: 243

Answers

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

Paused until further notice.

Posted 2010-06-22T05:09:32.457

Reputation: 86 075

cool I though it was a bug... :) – Johan – 2010-06-22T06:11:42.570

2Note Windows still uses // for network paths, so this may be a SMB compatibility thing too. – Fake Name – 2010-06-22T06:53:48.080

5Technically, Windows uses \ for UNC paths. – ThatGraemeGuy – 2010-06-22T07:13:53.537

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

Rich Homolka

Posted 2010-06-22T05:09:32.457

Reputation: 27 121