bash: PWD 和 CURDIR 有什么区别?

bash: What is the difference between PWD and CURDIR?

我的问题

我将 Makefile 用于 运行 一个 docker run 目标,它需要当前工作目录作为其参数之一。

我使用 $(PWD)$(CURDIR):

build: Dockerfile
        docker run ... <$(PWD) or $(CURDIR)>

它们似乎产生了相同的价值。我不知道是否有细微的差别以后会咬我,所以我想知道他们每个人的确切定义。

我尝试了什么

我的问题

Makefile 中的 $(PWD)$(CURDIR) 有什么区别?

TL;DR

使用CURDIR.

为什么?

首先感谢Renaud Pacalet for

CURDIR

引用 GNU Make Manual:

CURDIR

Set to the absolute pathname of the current working directory.

For your convenience, when GNU make starts (after it has processed any -C options) it sets the variable CURDIR to the pathname of the current working directory. This value is never touched by make again: in particular note that if you include files from other directories the value of CURDIR does not change. The value has the same precedence it would have if it were set in the makefile (by default, an environment variable CURDIR will not override this value). Note that setting this variable has no impact on the operation of make (it does not cause make to change its working directory, for example).

PWD

Make 手册中没有提及 PWD。快速 env | grep PWD 发现它是由环境设置的(在我的例子中是 zsh)。 GNU 关于 特殊 Shell 变量的注释 说明:

PWD

Posix 1003.1-2001 requires that cd and pwd must update the PWD environment variable to point to the logical name of the current directory, but traditional shells do not support this. This can cause confusion if one shell instance maintains PWD but a subsidiary and different shell does not know about PWD and executes cd; in this case PWD points to the wrong directory. Use ``pwd' rather than $PWD'.

由于 CURDIR 保证在 Make 中工作 PWD 可能 继承自 shell,前者应该是首选。