I'm new to using Makefiles & am puzzled by something…
I have the following in a Makefile…
USER_ID := $(id -u)
USER_ID_ESC := $$(id -u)
MAKEFILE := $(realpath $(lastword $(MAKEFILE_LIST)))
ROOT_DIR := $(dir $(MAKEFILE))
x:
echo $${USER_ID}
echo ${USER_ID}
echo $(USER_ID)
echo $${USER_ID_ESC}
echo ${USER_ID_ESC}
echo $(USER_ID_ESC)
echo $${ROOT_DIR}
echo ${ROOT_DIR}
echo $(ROOT_DIR)
exit 0
Which give the output…
echo ${USER_ID}
echo
echo
echo ${USER_ID_ESC}
echo $(id -u)
502
echo $(id -u)
502
echo ${ROOT_DIR}
echo /Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
/Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
echo /Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
/Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
exit 0
I understand the use of double $
to escape the use of $
, but what I don't understand why using a single $
for realpath
& dir
commands works, but using it for the id
command doesn't.
Can someone help me out in understanding?
Thanks
realpath
and dir
are GNU make
functions, id
isn't. If you want to use a shell function inside a Makefile, use $(shell id -u)
. realpath
and dir
aren't escaped, they are correctly interpreted by GNU make.
See GNU make file name functions as well as GNU make shell function manual pages.
When you write $(id -u)
in a Makefile, make
will try to look if it's a function or a variable, and since it's none of these it will not expand to anything, hence the empty echo
s.
Now the reason why your ID was correctly written when you did escape the dollar sign is mysterious and probably due to your shell. What happened is that make
didn't try to expand it since the $
is escaped, so echo $(id -u)
was sent to the system. You can try it directly in your shell, you should obtain the same result.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments