The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
1

Using $LD_PRELOAD (env variable) with $TERMINAL (i3 variable)

asked 2014-01-22 22:26:31 +0000

BatmanAoD gravatar image

I use stderred to color the stderr of programs that run in the terminal. This relies on the $LD_PRELOAD hook. Can I use the same trick to load stderred every time I open a new terminal, so that something like echo ha >&2 will output a red-colored ha? I tried setting the i3 $TERMINAL variable to be an entire Bash-like command (set $TERMINAL 'LD_PRELOAD=/net/erard/usr/lib/stderred/build/libstderred.so /usr/bin/konsole'), but of course that didn't work.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-01-22 23:34:12 +0000

Adaephon gravatar image

updated 2014-02-03 09:05:15 +0000

Handling of variables in the i3 config is very minimal, they are just set verbatim to whatever is written after the variable name. This includes quotation marks. That means, when you do

set $TERMINAL 'LD_PRELOAD=/net/erard/usr/lib/stderred/build/libstderred.so /usr/bin/konsole'

the single quotes are still contained in $TERMINAL. When you try to start the terminal, exec or rather the shell called by exec tries to start a binary named LD_PRELOAD=/net/erard/usr/lib/stderred/build/libstderred.so /usr/bin/konsole

To get the intended results, just define $TERMINAL like you would write it in the shell, that is, without surrounding quotes:

set $TERMINAL LD_PRELOAD="/net/erard/usr/lib/stderred/build/libstderred.so" /usr/bin/konsole

You could also create a script like this:

#!/bin/sh
export LD_PRELOAD="/net/erard/usr/lib/stderred/build/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}"
/usr/bin/konsole

Make it executable and then change the configuration accordingly

set `$TERMINAL` /path/to/script

Also going by the instructions on (stderred github page)[https://github.com/sickill/stderred] it is sufficient to put

export LD_PRELOAD="/absolute/path/to/stderred/buildlibstderred.so${LD_PRELOAD:+:$LD_PRELOAD}"

into your shell configuration (~/.bashrc, ~/.zshrc, etc.). That is because stderred changes the behaviour of the commands you are calling in the shell and not the the behaviour of the terminal or shell. Therefore it doesn't have to be already set when opening the terminal.


Im leaving the first answer, because, although it is not really necessary in the case of stderred, it may be of help in other cases where $LD_PRELOAD (or any other environment variable) has to be set for the program called.

edit flag offensive delete link more

Comments

Thank you, this is clever. Unfortunately, it appears that calling `konsole` with `LD_PRELOAD` set to point to libstderred.so doesn't actually color all stderr, anyway, so I suppose the issue is moot.

BatmanAoD gravatar imageBatmanAoD ( 2014-01-23 17:51:44 +0000 )edit

That is strange, I just tested it (Ubuntu 13.10, libstderred fresh from git) and it worked fine with konsole. I did `cat nonexistantfile` and the error message was red.

Adaephon gravatar imageAdaephon ( 2014-01-24 09:42:32 +0000 )edit

`cat nonexistantfile` makes a red error message regardless of whether Bash itself was started with $LD_PRELOAD set. What I want is for bash scripts that use "echo message >&2", or even the command "echo ha >&2" in an interactive session, to create red output. Maybe stderred just doesn't do this?

BatmanAoD gravatar imageBatmanAoD ( 2014-01-28 22:48:35 +0000 )edit

Output redirections work by changing which stream a file descriptor indicates not by changing the fd a program uses. By default 1=stdout and 2=stderr. Using >&2 both, 1 and 2, indicate stderr. stderred only works with 2, while echo only writes to 1 both not knowing that 1 points to stderr, too.

Adaephon gravatar imageAdaephon ( 2014-01-29 11:49:08 +0000 )edit

Ah. Well, that makes sense. Thanks. Do you know of any simple command-line utilities like echo that print to fd 2 instead of fd 1?

BatmanAoD gravatar imageBatmanAoD ( 2014-01-29 17:59:48 +0000 )edit

Question Tools

Stats

Asked: 2014-01-22 22:26:31 +0000

Seen: 997 times

Last updated: Feb 03 '14