为什么“.myscript”return file-not-found in only one Git for Windows sh.exe?

Why does ". myscript" return file-not-found in only one Git for Windows sh.exe?

我已经从 here 下载了 Git。

我最近发现 sh.exe 有两个副本,其中提供了 bash shell。

  1. C:\Program Files\Git\bin\sh.exe
  2. C:\Program Files\Git\usr\bin\sh.exe

我发现两者在功能上存在差异。

例如,#1 实例可以通过以下方式 运行 bash 脚本:. myScript.sh 其中 #2 实例要求语法为:. ./myScript.sh #如果使用#1 语法,2 实例将报告丢失文件错误,即使文件存在。

  1. 为什么有两个实例?
  2. 哪一个是 "proper" 那个?
  3. 是否有关于这些实现之间差异的文档?
  4. 我有什么强烈的理由不应该使用其中之一吗?
  5. 功能上的差异是错误吗?

为什么要使用多个 Shell?

这看起来你有一份 dash(一个最小的 /bin/sh 实现 -- 快速启动,很少有花哨的东西)和一份 bash(一个大得多 shell,大量扩展来自 ksh)。当以 sh 名称调用时,bash 会禁用 一些 额外功能,但不是全部。

其中一个 "features" bash 添加到基线 POSIX sh 正在搜索当前目录以查找来自 .source 命令的文件,而不是像 POSIX sh 规范要求的那样只搜索 PATH


它们为什么不同?谁是正确的?

相关标准在https://pubs.opengroup.org/onlinepubs/009695399/utilities/dot.html;特别参见基本原理部分的第一段:

Some older implementations searched the current directory for the file, even if the value of PATH disallowed it. This behavior was omitted from this volume of IEEE Std 1003.1-2001 due to concerns about introducing the susceptibility to trojan horses that the user might be trying to avoid by leaving dot out of PATH .

除了 PATH 中的目录外,shell 还搜索了 .。但是,出于安全原因,POSIX sh 规范不要求这种行为;未将 . 放入 PATH 的用户可能希望能够 cd 进入他们无法控制或信任其内容的目录,而不必担心域名仿冒或其他攻击。

假设包含 myscript.sh 的目录不在 PATH 中,报告 "file not found" 的 shell 比另一个更严格。