Colorful output in Makefile

Hello! Here is my Makefile:

.PHONY: all
all: build run clean

.PHONY: build
build:
	@echo Bulding...
	@javac -classpath .:target/dependency/* -d . *.java 2> /dev/null

.PHONY: run
run:
	@echo Running...
	@echo Program output:
	@java -classpath .:target/dependency/* Main

.PHONY: clean
clean:
	@echo Cleaning up...
	@rm -rf *.class

I need to emphasize messages like Building..., Running... with some colors. I’ve tried to modify my code as follows but it didn’t help me:

NO_COLOR=\x1b[0m
OK_COLOR=\x1b[32;01m
ERROR_COLOR=\x1b[31;01m
WARN_COLOR=\x1b[33;01m

OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)
ERROR_STRING=$(ERROR_COLOR)[ERRORS]$(NO_COLOR)
WARN_STRING=$(WARN_COLOR)[WARNINGS]$(NO_COLOR)

.PHONY: all
all: build run clean

.PHONY: build
build:
	@echo $(OK_COLOR)Bulding...$(NO_COLOR)
	@javac -classpath .:target/dependency/* -d . *.java 2> /dev/null

.PHONY: run
run:
	@echo Running...
	@echo Program output:
	@java -classpath .:target/dependency/* Main

.PHONY: clean
clean:
	@echo Cleaning up...
	@rm -rf *.class

What am I doing wrong?

I am not so good with bash variables, but I think you are supposed to #!/bin/bash before the variables, like this

Screenshot 2022-10-09 2.05.32 PM

But if this doesn’t work can you attach a picture of the error, as I am not completely clear on what it is.

Use e directly instead of \033

1 Like