Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

lz4 command outputs to stdout unexpectedly

Writer Emily Wong

Using this lz4 command:

$ apt search ^lz4$
Sorting... Done
Full Text Search... Done
lz4/focal,now 1.9.2-2 amd64 [installed,automatic] Fast LZ compression algorithm library - tool
$ lz4 --version
*** LZ4 command line interface 64-bits v1.9.2, by Yann Collet ***

This command normally creates a filename.lz4 file (just like gzip does):

$ lz4 -9 -k filename

However, this command does not, and instead writes to stdout when I'm not telling it to:

$ t=$(lz4 -9 -k filename)
Warning : using stdout as default output. Do not rely on this behavior: use explicit `-c` instead !
bash: warning: command substitution: ignored null byte in input

Why is it doing this? Is it a bug, or is there a reason documented somewhere?

1 Answer

The behaviour appears to be a feature - not a bug. I can't find it documented anywhere except in the source code:

/* No output filename ==> try to select one automatically (when possible) */
while ((!output_filename) && (multiple_inputs==0)) { if (!IS_CONSOLE(stdout)) { /* Default to stdout whenever stdout is not the console. * Note : this policy may change in the future, therefore don't rely on it ! * To ensure `stdout` is explicitly selected, use `-c` command flag. * Conversely, to ensure output will not become `stdout`, use `-m` command flag */ DISPLAYLEVEL(1, "Warning : using stdout as default output. Do not rely on this behavior: use explicit `-c` instead ! \n"); output_filename=stdoutmark; break; . .
}

On Linux, IS_CONSOLE uses isatty to determine whether file desscriptors are connected to a terminal.

As suggested in the comment, you can use the -m option to force generation of an output file in cases where there is no tty:

t=$(lz4 -9 -k -m filename)
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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