bash: What is the difference between PWD and CURDIR?
Asked Answered
A

1

29

My Problem

I use a Makefile to run a docker run target, which needs the current working directory as one of its parameters.

I use either $(PWD) or $(CURDIR):

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

They seem to be producing the same value. I don't know if there's a subtle difference that can bite me later, so I want to know the exact definition of each of them.

What Have I Tried

  • STFW
  • man make

My Question

What is the difference between $(PWD) and $(CURDIR) in a Makefile?

Analgesic answered 21/9, 2018 at 6:20 Comment(4)
If you are using GNU make you should probably: 1. Look at its documentation and search for the CURDIR make variable. 2. PWD is not a built-in make variable. It comes from the environment and is passed to make as if it was a make variable. So, look maybe at its definition in your shell's manual (e.g. man bash).Handicapped
Fantastic. CURDIR it is. Don't know how I missed that gmake link. Write it as an answer and I'll be happy to accept.Analgesic
This is not really an answer, just links to where to find answers. If you wish, answer maybe yourself by summarizing your findings.Handicapped
With pleasure, with credit.Analgesic
A
55

TL;DR

Use CURDIR.

Why?

First, thanks Renaud Pacalet for his comment.

CURDIR

Quoting the 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

There's no reference to PWD in the Make manual. A quick env | grep PWD found that it was set by the environment (in my case, zsh). GNU's notes about Special Shell Variables state that:

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

Since CURDIR is guaranteed to work in Make in PWD might be inherited from the shell, the former should be preferred.

Analgesic answered 21/9, 2018 at 15:40 Comment(2)
The origin of variables can be easily checked with $(origin ...): cat Makefile && make $(info CURDIR is from $(origin CURDIR)) $(info PWD is from $(origin PWD)) CURDIR is from file PWD is from environmentCitole
CURDIR will eventually become the portable standard for make, while PWD is still a UNIX-ism which may fail in classic Command Prompt and PowerShell contexts.Forging

© 2022 - 2024 — McMap. All rights reserved.