1

For background, I previously asked this question.

I am using stderred to turn stderr text in red when diplayed on a terminal. This works nicely on most occasions, more specifically for what appear to be binary executables as opposed to bash scripts.

I realized that stderred does not appear to work when stderr is referred to directly from a command called from a terminal.

For example, using stderred I still do not have red if I use redirection as with the following:

echo "pomme" 1>&2

How is redirection any different than other cases with regards to the stderred hook?

How can I have red for stderr output if I redirect 1>&2 in a /bin/bash context, as I do in my bash scripts (Ubuntu 15.10)?

muru
  • 197,895
  • 55
  • 485
  • 740
Tfb9
  • 681
  • 4
  • 13
  • 33
  • The terminology is confusing ... you haven't shown any instance of a pipe being used. Where's the pipe? – muru Dec 31 '15 at 19:55
  • If you're trying to preserve colors when output is redirected to a file, then you could consider using the script program instead – steeldriver Dec 31 '15 at 21:32

1 Answers1

4

If you look at stderred's README, you'll note that it modifies the write() function (and associated stream functions):

stderred hooks on write() and a family of stream functions (fwrite, fprintf, error...) from libc in order to colorize all stderr output that goes to terminal thus making it distinguishable from stdout. Basically it wraps text that goes to file with descriptor "2" with proper ANSI escape codes making text red.

Emphasis mine.

When you redirect stdout to stderr, they point to the same thing, true. However, the function calls that wrote to stdout still refer to it as stdout, no matter what redirection you do. They still access file descriptor 1, not 2.

If you want to write to stderr, and have it coloured, normal shell redirection will not suffice. You'll need to use something that writes directly to stderr. The awk function in the similar Unix & Linux post can be adapted to work with multiple arguments:

error () (
  IFS=' '
  awk -v msg="$*" 'BEGIN { print msg > "/dev/stderr" }'
)

I used /dev/stderr since that's easier to read and mentioned in the GNU awk docs. Also note that I have used ( ) instead of { } for command grouping. This causes the commands to be run in a subshell, so I can safely modify IFS without the calling shell or script being affected.

References:

muru
  • 197,895
  • 55
  • 485
  • 740
  • The proposed stdred function transforms all output in red, which is no longer a way to differentiate visually stderr from stdout... the true purpose of all this (e.g. when reading the screen output of a script that writes to both stderr and stdout ). – Tfb9 Dec 31 '15 at 20:32
  • @Tfb9 I don't know what your "true" purpose is. What I see is a question that asks why redirecting stdout to stderr didn't change stdout's colour. That's why I said: "If you want stdout to be coloured red as well, in addition to stderr". Maybe you should spent a little more time framing your question? – muru Dec 31 '15 at 20:34
  • I am trying to redirect something to stderr and have it coloured in the terminal window. It is based on this https://stackoverflow.com/questions/2990414/echo-that-outputs-to-stderr – Tfb9 Dec 31 '15 at 20:39
  • And as my answer says, with redirection, the command is still writing to stdout - it's just that stdout points to stderr. Can you update your question with an actual use case? – muru Dec 31 '15 at 20:43
  • Excuse my noobinesss, but I do not understand what you mean by 'the command is still writing to stdout - it's just that stdout points to stderr'. I'd like to know more about how my stderr which is pointed at does not show in red. – Tfb9 Dec 31 '15 at 21:00
  • Are you familiar with any programming languages? – muru Dec 31 '15 at 21:03