在 Bash 中,cd ./someDir 和 cd someDir 之间有什么实际区别吗?

In Bash, is there any practical difference between cd ./someDir and cd someDir?

在终端中,输入 cd ./someDircd someDir 获得相同的结果,即从当前目录切换到 someDir 子目录。

在哪些情况下您会使用一个命令而不是另一个命令,或者这两个命令完全等同?

man cd 并没有阐明这个问题,我也不容易在网上找到文档。某处是否有相关文档?

我是 运行 MacOS Catalina 中的标准 shell VSCode,$SHELL --version 报告 GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)

这是我的第一个问题,我愿意接受有关如何改进它的任何反馈。谢谢。

编辑:这与我遇到的特定用例无关,我只是想知道这些命令之间是否存在任何实际差异。

它最近出现在一个 PR 中,我看到 cd ./ 被使用,我问他们为什么使用它而不是 cd 本身。他们明白前者“确保它被限制在当前目录”,但无法进一步解释。

从表面上看,这两个命令是相同的,但在 Bash 中,细微的差异会导致不同的行为,我想知道这里是否属于这种情况。通常我会单独使用 cd 。我理解 . 是对当前目录的引用。

如果设置了 $CDPATH,这两个命令的行为会有所不同。

cd ./dir

更改为 ./dir,没有其他更改,但是

cd dir
如果 some/path$CDPATH 中,

可能会更改为 some/path/dir

is there any practical difference between cd ./someDir and cd someDir?

嗯,不在我的账户上。但是是的。

In which situations would you use one over the other

我会用任何一个。

, or are the two commands exactly equivalent?

没有

Are there docs covering this somewhere?

来自POSIX cd

  1. If the first component of the directory operand is dot or dot-dot, proceed to step 6.

  2. Starting with the first pathname in the -separated pathnames of CDPATH (see the ENVIRONMENT VARIABLES section) if the pathname is non-null, test if the concatenation of that pathname, a character if that pathname did not end with a character, and the directory operand names a directory. If the pathname is null, test if the concatenation of dot, a character, and the operand names a directory. In either case, if the resulting string names an existing directory, set curpath to that string and proceed to step 7. Otherwise, repeat this step with the next pathname in CDPATH until all pathnames have been tested.

  3. ...

所以:

$ mkdir -v -p a tmp/a
mkdir: created directory 'a'
mkdir: created directory 'tmp'
mkdir: created directory 'tmp/a'
$ CDPATH=$PWD/tmp

然后:

$ cd a
/some/path/tmp/a

但是:

$ cd ./a
$ pwd
/some/path/a