nor*_*bjd 7 makefile gnu-make gnome-terminal ansi-colors
我正在尝试从以下打印粗体文本Makefile:
printf-bold-1:
@printf "normal text - \e[1mbold text\e[0m"
Run Code Online (Sandbox Code Playgroud)
但是,转义序列按原样打印,因此在运行时make printf-bold-1,我得到:
普通文本 - \e[1mbold text\e[0m
而不是预期:
普通文本 -粗体文本
这很奇怪,因为我可以从我的终端打印粗体文本:printf "normal text - \e[1mbold text\e[0m"按预期直接运行命令会产生:
普通文本 -粗体文本
在 中Makefile,我尝试使用@echoorecho代替@printf,或打印\x1b代替\e,但没有成功。
以下是一些描述我的环境(带有标准 Gnome 终端的 Linux)的变量,如果可以的话:
COLORTERM=gnome-terminal
TERM=xterm-256color
Run Code Online (Sandbox Code Playgroud)
另请注意,在某些同事的笔记本电脑 (Mac) 上,可以正确打印粗体文本。
在各种环境下从Makefile规则打印粗体或彩色文本的便携方式是什么?
您应该使用通常的tput程序为实际终端生成正确的转义序列,而不是硬编码特定字符串(例如,在 Emacs 编译缓冲区中看起来很难看):
printf-bold-1:
@printf "normal text - `tput bold`bold text`tput sgr0`"
Run Code Online (Sandbox Code Playgroud)
当然,您可以将结果存储到 Make 变量中,以减少子 shell 的数量:
bold := $(shell tput bold)
sgr0 := $(shell tput sgr0)
printf-bold-1:
@printf 'normal text - $(bold)bold text$(sgr0)'
Run Code Online (Sandbox Code Playgroud)
好,我知道了。我应该使用\033而不是\eor \x1b:
printf-bold-1:
@printf "normal text - \033[1mbold text\033[0m"
Run Code Online (Sandbox Code Playgroud)
或者,按照评论中的建议,使用简单引号而不是双引号:
printf-bold-1:
@printf 'normal text - \e[1mbold text\e[0m'
Run Code Online (Sandbox Code Playgroud)
make printf-bold-1现在生产:
普通文本 -粗体文本