bash `cd //` 中的双斜杠 // 在 Linux 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20690828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:02:32  来源:igfitidea点击:

What does double slash // in `cd //` mean in Linux?

linuxbashshelldirectory-structurecd

提问by vitaut

I've entered a command cd //instead of cd /by mistake and instead of getting an error as I'd expect the shell (Bash) showed a prompt as if I'm in the //directory:-

我输入了一个命令,cd //而不是cd /错误地输入了一个命令,而不是像我期望的那样,shell (Bash) 显示一个提示,就好像我在//目录中一样:-

<username>@<hostname>://$
<username>@<hostname>://$ pwd
//

Listing the content of the directory shows that it is the same as /.

列出目录的内容表明它与/.

So what exactly cd //do and what's the difference from cd /?

那么究竟是cd //做什么的,和有什么区别cd /

回答by Rahul Tripathi

Actually it means nothing and is ignored.

实际上它没有任何意义并且被忽略。

From the Bash FAQ E10::

来自Bash 常见问题 E10

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

E10) 为什么 'cd //' 将 $PWD 保留为 '//'?

POSIX.2 在其对 'cd' 的描述中说,在规范化当前工作目录时,三个或更多前导斜杠可以替换为单个斜杠。

我认为这是为了历史兼容性。某些版本的 Unix 和早期的网络文件系统使用 //hostname/path 形式的路径来访问服务器“主机名”上的“路径”。

Also the Unix standardsstates:

另外,Unix的标准规定:

A pathname that begins with two successive slashes may be interpreted in an implementation-defined manner, although more than two leading slashes shall be treated as a single slash.

以两个连续斜杠开头的路径名可以以实现定义的方式解释,但两个以上的前导斜杠应视为单个斜杠。

回答by Serpentine Cougar

This answeron AskUbuntu confirms that they are the same directory. Further, it helpfully quotes from the POSIX Pathname Resolution specification:

AskUbuntu 上的这个答案确认它们是相同的目录。此外,它有用地引用了POSIX 路径名解析规范

A pathname consisting of a single slash shall resolve to the root directory of the process. A null pathname shall not be successfully resolved. A pathname that begins with two successive slashes may be interpreted in an implementation-defined manner, although more than two leading slashes shall be treated as a single slash.

由单个斜杠组成的路径名应解析为进程的根目录。不应成功解析空路径名。以两个连续斜杠开头的路径名可以以实现定义的方式解释,但两个以上的前导斜杠应视为单个斜杠。