0

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

Jinyu
  • 169
  • 2
  • 9
  • 1
    The `echo TERM2=...` recipe must be valid shell syntax. If your make variables `BG_RED`, `TERM` or `_OFF_` expand to characters that make the recipe invalid you get errors when the recipe is executed. Very likely one of these shell variables contain parentheses. Try to single-quote the string: `echo TERM2='${BG_RED}${TERM}${_OFF}_xxx'`. – Renaud Pacalet Dec 09 '21 at 07:15
  • It works on Linux. It would be helpful to include the output of the two tput commands piped to `od -a`. Otherwise agree with @RenaudPacalet to quote the value. – Allan Wind Dec 09 '21 at 07:20
  • Perhaps not related to your problem, but you claim to use bash; however, the error message does not come from _bash_, but from _sh_. – user1934428 Dec 09 '21 at 07:26
  • @allan-wind I tried on Amazon Linux 2 and it has same error. the `tput sgr0 | od -a` on linux and my mac are same ```0000000 esc ( B esc [ m 0000006``` @renaud-pacalet it works if quote it. Thanks a lot – Jinyu Dec 09 '21 at 07:43
  • @Jinyu update question with new data instead of a comment. – Allan Wind Dec 09 '21 at 07:45

2 Answers2

1

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
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0

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
Jinyu
  • 169
  • 2
  • 9