./*/ 是可移植的吗?

Is ./*/ portable?

我经常在 for 循环中使用 ./*/,例如

for d in ./*/; do
  : # do something with dirs
done

匹配当前工作目录中的所有非隐藏目录,但我不确定这是否是一种可移植的方式。我的系统上安装了 bash、dash 和 ksh,它适用于所有系统,但由于 POSIX spec doesn't say anything about it (or it says implicitly, and I missed it) I think I can't rely on it. I also checked POSIX bug reports,但无济于事,那里也没有提及它。

它的行为实现或文件系统依赖吗?我在这里错过了什么吗?我怎么知道它是否便携?

简短回答:是

长答案:

POSIX 标准(来自 opengroup)指出 / 将只匹配扩展文件名中的斜杠。由于 Unix/Linux 不允许在文件名中使用 /,我相信这是 Unix/Linux 系统上的安全假设。

从下面的粗体文本来看,似乎即使对于允许在文件名中使用 / 的系统,POSIX 标准也要求 / 不会匹配到这样的文件。

在Windows上,文件名中似乎不允许出现/,但我不是Windows上的专家。

来自Shell Programming Language § Patterns Used for Filename Expansion

The slash character in a pathname shall be explicitly matched by using one or more slashes in the pattern; it shall neither be matched by the asterisk or question-mark special characters nor by a bracket expression. Slashes in the pattern shall be identified before bracket expressions; thus, a slash cannot be included in a pattern bracket expression used for filename expansion. ...

附加说明 - 阐明路径名:

路径名在 4.13 中定义,在 General Concepts § Pathname Resolution.

中明确引用带有尾部斜线的路径名

A pathname that contains at least one non-<slash> character and that ends with one or more trailing <slash> characters shall not be resolved successfully unless the last pathname component before the trailing <slash> characters names an existing directory or a directory entry that is to be created for a directory immediately after the pathname is resolved. Interfaces using pathname resolution may specify additional constraints when a pathname that does not name an existing directory contains at least one non-<slash> character and contains one or more trailing <slash> characters.