$(shell tput sgr0) in Makefile cause "syntax error near unexpected token `('"

337 views Asked by At

I have a Makefile with tput color variables

_OFF := $(shell tput sgr0)
BG_RED := $(shell tput setab 1)

$(info TERM1=${BG_RED}${TERM}${_OFF}_xxx)

all:
    @echo TERM2=${BG_RED}${TERM}${_OFF}_xxx

The second prompt will give out an error

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `echo TERM2=xterm-256color_xxx'
make: *** [all] Error 2

The interesting thing is even it report an error, but the word xterm-256color in error message /bin/sh: -c: line 0: `echo TERM2=xterm-256color_xxx'` actually has red background color, and the following _xxx has normal background color.

Is it a bug? or is there anything wrong to my code?

I am using MacOS 11.6.1, GNU bash 3.2.57(1)-release (x86_64-apple-darwin20), GNU Make 3.81, tput ncurses 5.7.20081102

2

There are 2 answers

0
Jinyu On

Thanks for the comment from @Renaud Pacalet and @Allan Wind. Quote the echo string can fix the syntax error.

BG_RED := $(shell tput setab 1)
_OFF := $(shell tput sgr0)

$(info TERM1=${BG_RED}${TERM}${_OFF}_xxx)

all:
    @echo 'TERM2=${BG_RED}${TERM}${_OFF}_xxx'

According to the hint from @Allan Wind, I think the problem is there is a ( in tput sgr0 output

$ tput sgr0 | od -a                                                                                                                 
0000000  esc   (   B esc   [   m
0000006
0
Allan Wind On

Quote the value being set as your tput sgr0 returns a (:

@echo TERM2='${BG_RED}${TERM}${_OFF}_xxx'

If you want make to use bash instead of sh, then you need the SHELL variable:

SHELL=/bin/bash