-3

You can colour ttys by doing things like this

myscript.sh:

#!/bin/bash
echo -e "This is \033[0;31mRED\033[0m"

Often if another "parent" program runs myscript.sh as a sub-process then the colour codes will get correctly passed back and the shell running the parent will show the colours of the child.

Often it doesn't work. GNU Make is a counter-example

Makefile:

foo:
    ./myscript.sh

If I run make foo then the output is

This is \033[0;31mRED\033[0m

For some reason, make feels the need to escape the output of child processes.

I find this surprising since whoever wrote make would have had to add code, which is extra effort, to create an anti-feature. Or is it the case that applications such as make throw a switch on the tty which makes everything get escaped automagically? If so can I override this behaviour and force programs such as make to pass through child process data to the tty without transforming it? Or does each program control this behaviour its own way?

2

The problem is that your script relies upon a non-POSIX bash feature, but make is using /bin/sh for the shell, which (for instance, Debian) may be a different program such as dash (a more POSIX-compliant shell).

Further reading:

  • Having extra features does not make a shell non-compliant. Additionally, the POSIX spec explicitly allows backslashes to be interpreted. – Patrick Oct 16 '16 at 5:20
  • I just ran echo -e "This is \033[0;31mRED\033[0m" in sh, the colours came out fine. – spraff Oct 16 '16 at 9:56
1

As Thomas mentions in his answer, the shell being used by make is different than the shell you are using when you run the script manually.

As a general rule, if you're using features specific to a certain shell within a script, you should put that specific shell in the shebang line.

For example:

#!/bin/bash
echo -e "This is \033[0;31mRED\033[0m"
  • My script already did that. Updated the question. – spraff Oct 16 '16 at 9:57
-1

The script called by make needs to use the filesystem echo, not the builtin one

/bin/echo -e "\033...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.