I'm using stderred to color all output streamed to stderr red. It's working fine. But when I write my own bash script and throw an error with echo 'error' 1>&2, it doesn't color the output in red. I reckon this is, because the command simply redirects the output to wherever the stderr file descriptor points to, but doesn't properly mark the message as belonging to stderr. Is that so? How can I properly write to stderr in bash?

  • 2
    for the record, you can also do >&2 echo error according to this answer. – Erik Kaplun Dec 14 '15 at 14:52

It appears that the program is re-writting the various write() functions to detect whether you are printing to file descriptor 2 and then adding the relevant escape codes to make the output red at the terminal.

Unfortunately, in shell, when you do something like

echo "foo" 1>&2 

The function will still be calling write (or some other similar system call) on file descriptor 1. The output appears on fd 2 since file descriptor 1 has been dupped to file descriptor 2 by your shell.

Unfortunately, I don't know of way to write /directly/ to fd 2 in shell, but you can use awk. A function like this will write the arguments directly to file descriptor 2.

error() {
  awk " BEGIN { print \"$@\" > \"/dev/fd/2\" }"

I believe this is a feature of GNU awk that isn't part of POSIX but it also works on the awk provided on OS X.

| improve this answer | |
  • Sounds like stderred is a nice idea implemented wrong. The approach I would have taken would be to create a new pts device and connect file descriptor 2 to that. Then a it could have a process read the data written to that file descriptor and rewrite it to the original pts with escape codes included. Then it wouldn't have needed to use LD_PRELOAD, and 1>&2 would have worked. (My approach probably wouldn't have been entirely without problems either, but I think it would have worked better.) – kasperd Oct 25 '14 at 22:35

That software hooks the write() system calls that apprear to write at file descriptor 2, that is known as stderr.

It is a shared library (so recompilation of the kernel is not necessary). As descripted in the installation manual it takes use of the environment varaible LD_PRELOAD. The dynamic linker can be influenced into modifying its behavior during the program's execution. These variables adjust the runtime linking process by searching for shared libraries at alternate locations.

The executable /bin/bash itself does not honour such variables, so bash itself uses the original pallette of system calls, not the modified ones. The redirection you mentioned in your question happend in bash. Not in the executable of echo. If echo could wirte to the error descriptor by itself (not via bash) the output would be red.

Python can write to stderr. See the proof (that should appear in red):

python -c 'import os; os.write(2, "error")'
| improve this answer | |

Create a helper function:

echoerr() { printf "\033[0;31m%s\n\033[0m" "$*" >&2; }

And use it anywhere:

echoerr "something went wrong here"

This combines writing to stderr (>&2) and changing colors (solution taken from here https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux).

| improve this answer | |
  • You may want to test whether the standard error goes to a terminal before doing colour output. – Kusalananda May 17 '18 at 8:08
  • @Kusalananda TBH, I don't know how to do that, so I did not. Also, it's a function, so the real destination can change. Considering the question asked, not sure overall, could be just fine. – VasyaNovikov May 17 '18 at 8:20

According to instruction from https://github.com/sickill/stderred you should add to your script

export LD_PRELOAD="/path/to/stderred/\$LIB/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}"

where the $LIB may should be substituted by hand onto you architecture

| improve this answer | |

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.