0

I want every pipe to be unbuffered, so I don't have to type stdbuf -oL for every piped command. When concocting commands with multiple pipes, it would be nice if there was a environment variable or something to enable it globally or at least for the remainder of the pipes in the command.

Yes I know about unbuffer and stdbuf but they need to be invoked for every pipe... I'm trying to save typing because I do this often.

Something like:

before:

stdbuf -oL command_1 | stdbuf -oL command_2 | stdbuf -oL command_3

after:

BUFFERING=-oL command_1 | command_2 | command_3

1 Answers1

1

The stbuf utility in GNU Coreutils works by setting the LD_PRELOAD environment variable to force all descendant processes to open the libstdbuf.so shared library. In addition, it sets a few other variables to communicate parameters to that library.

You can just set those environment variables yourself.

Let's use env to see what they are:

$ stdbuf -oL env | grep -E '^LD_PRELOAD|^_STDBUF_'
_STDBUF_O=L
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/coreutils/libstdbuf.so

OK, so if we just eval the above, we got it:

$ eval $(stdbuf -oL env | grep -E '^LD_PRELOAD|^_STDBUF_')
$ echo $LD_PRELOAD 
/usr/lib/x86_64-linux-gnu/coreutils/libstdbuf.so
$ echo $_STDBUF_O 
L

One more thing; we have not told the shell to export these:

$ export LD_PRELOAD _STDBUF

Now the C stdout stream should be unbuffered in programs we run.

It seems simpler, however, to just run a nested shell with setbuf:

$ stdbuf -oL bash
$ echo $LD_PRELOAD
/usr/lib/x86_64-linux-gnu/coreutils/libstdbuf.so

exec can be used to replace the original shell isntead:

$ exec stdbuf -oL bash
Kaz
  • 7,676
  • 1
  • 25
  • 46