CURDIR Make Bash

What CURDIR is used for

CURDIR is the preferred way to refer to the current directory.

Here is what is written in the official documentation

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

Example

An example of printing the current directory to the screen.

GNUmakefile is created in the directory

/home/andrei/sandbox

vi /home/andrei/sandbox/GNUmakefile

.PHONY: show-curdir show-curdir: @echo "CURDIR is:" @echo ${CURDIR}

make show-curdir

CURDIR is:
/home/andrei/sandbox

Check whether a directory exists

You need to check whether the sandbox subdirectory cpp with C++ code exists or not

For this you need the command -d

.PHONY: check-cpp check-cpp: @if [ -d ${CURDIR}/cpp ]; then \ echo ${CURDIR}/cpp" exists"; \ else echo ${CURDIR}/cpp" does not exist"; \ fi

make check-cpp

/home/andrei/sandbox/cpp does not exist

mkdir cpp
make check-cpp

/home/andrei/sandbox/cpp exists

Article author: Andrei Olegovich

Banner image