I have a Makefile that has a variable that needs to have a default value in case when variable is unset or if set but has null value.

How can I achieve this?

I need this, as I invoke make inside a shell script and the value required by the makefile can be passed from the shell as $1. And to pass this to makefile I have to set it inside bash-script.

Ideas: (Not elegant) Inside the bash script the variable could be checked if its set but has null value, in that case it can be unset.


Note: the following won't work if the variable are not defined in the terminal, as they are set in the bash script.


dSourceP?=$(shell pwd)
Source?=$(notdir $(wildcard $(dSourceP)/*.md))

Bash Script

make all dSourceP="${1}" Source="${2}"


bash ./MyScript.sh
bash ./MyScript.sh /home/nikhil/MyDocs
bash ./MyScript.sh /home/nikhil/MyDocs index.md

1 Answer 1


Since you’re using GNU make, you could use the ?= operator:

FOO ?= bar

but that doesn’t deal with pre-existing null (or rather, empty) values. The following deals with absent and empty values:

ifndef FOO
override FOO = bar

    echo "$(FOO)"

.PHONY: test

(Make sure line 6 starts with a real tab.)

You’d call this using

make FOO=blah

to set a value. make or make FOO= will end up setting FOO to bar; you need override to override variables set on the command-line.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.