0

I've got a problem with ANSI escape codes in my terminal on OpenSuse 13.2. My Makefile use to display pretty colors on OSX at work but at home when I use it I get the litteral termcaps such as \033[1;30m ... \033[0m

I know close to nothing about termcaps, I just found these escape characters that seemed to be working fine ! The strangest is that both my OSX and Linux terminal are configured with TERM=xterm-256color so I really don't know where to look for the correct setting I'm currently missing on Linux.

TL;DR: How to get escape codes such as \033[1;30m working in Konsole with xterm-256color ?

Edit: Here's a snippet of the Makefile I am talking about: \Here's a snippet of the Makefile I am talking about:

# Display settings
RED_L = \033[1;31m
GREEN_L = \033[1;32m
GREEN = \033[0;32m
BLUE = \033[0;34m
RED = \033[0;31m

all: $(OBJ_DIR) $(NAME)

$(OBJ_DIR):
        @mkdir -p $(OBJ_DIR)

$(NAME): $(OBJ)
        @echo "$(BLUE)Linking binary $(RED)$(NAME)$(BLUE).\n"
        @$(CC) -o $@ $^ $(LFLAGS)
        @echo "\t✻ $(GRAY)$(CC) -o $(RED)$(NAME)$(GRAY) object files:$(GREEN) OK! √\n$(NC)
cfz42
  • 384
  • 1
  • 14
  • Getting *literal* codes such as you mention is probably due to differences in the shell (or make-program). But your question does not show an example which someone can test. – Thomas Dickey Mar 21 '15 at 11:03

2 Answers2

0

The example which you gave does not rely upon the setting of TERM (unless it is going someplace other than the terminal, e.g., via some program which interprets it such as the ls program, which has its own notion about colors). It would help if you quoted the section of the makefile which uses the escape sequences. Without that, we can offer only generic advice, e.g,. by assuming you have an echo command in the makefile.

The place to start looking is at the shell which your makefile uses. One would expect bash to be the default shell on OpenSUSE. But suppose you are actually using some other shell which happens to not recognize the syntax you are using, and trying to do something like

echo '\033[1;34mhello\033[m'

To help ensure that you are using the expected shell, you can put an assignment in your makefile, e.g.,

SHELL = /bin/sh

This assumes that /bin/sh itself is going to work as intended. However, that is commonly a symbolic link (for Linux) to the real shell. If so, one possible solution would be to change the real shell using OpenSUSE's update-alternatives feature to change the shell to bash (or zsh).

For additional information, see the discussion of SHELL in the GNU make manual.

Reflecting comments on the version of make -- GNU make 4.0 is known to have incompatible changes versus 3.81, as noted in the thread GNU Make 4.0 released on LWN.net. In particular, there are several comments relating to your problem, starting here.

However, checking a recent Fedora, it seems that the problem really is that the default behavior for echo has changed. As noted in other discussions (such as Why doesn't echo support “\e” (escape) when using the -e argument in MacOSX), this was done to improve POSIX compatibility. You can get your colors back by adding a -e option to the echo commands.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Strangely it's not working with `SHELL = /bin/sh` but I get some of the colors with `SHELL = /bin/zsh` Any clue where to find the correct setting ? Looked up zshrc and didn't find anything interesting, I'm currently looking through bash configuration file to avoid me having this SHELL variable set everytime I want a colored Makefile – cfz42 Mar 21 '15 at 11:41
  • I think the solution is outside bash. What shell *is* `/bin/sh` ? – Thomas Dickey Mar 21 '15 at 11:51
  • Ah yes I was thinking about bash instead of sh (though it doesn't work with /bin/bash nor /bin/sh) my bad, I'm not quite sure to understand what you mean by what shell is /bin/sh, isn't it.. sh ? – cfz42 Mar 21 '15 at 12:44
  • You can see (partly) by doing `ls -l /bin/sh` and seeing what the symbolic link points to. – Thomas Dickey Mar 21 '15 at 13:29
  • For what it's worth, your example works for me with bash 4.1.5 and make 3.81, as well as zsh 4.3.10 and dash 0.5.5.1 – Thomas Dickey Mar 21 '15 at 13:37
  • sh points to bash, I have make 4.0 and bash 4.2 and it's not working :/ – cfz42 Mar 21 '15 at 14:21
  • whenever possible, try to use tput to use color, it pretty much guarentees you will not get odd characters if the terminal does not support it, and as a bonus you can also use all those other features too, I usually put my codes into variables (short-lived of course): – osirisgothra Apr 23 '15 at 11:00
  • RED="$(tput setaf 1)" RESET="$(tput sgr0)" and you can use the 256 color palette without having to do 38;5;x: BGDARKGREY="$(tput setab 238)". You can also use tput to check if the code was supported, it will return 1 if not, silently (as long as it's a bona fide code, ie: tput setaf = silent error, tput fairy = error message) – osirisgothra Apr 23 '15 at 11:11
0

I finally found the solution:

the problem was I used echo instead of echo -e which seems to be the default behaivour on Mac OSX.

Thanks for your help though, it lead me to good lectures :)

cfz42
  • 384
  • 1
  • 14