jq icon indicating copy to clipboard operation
jq copied to clipboard

stderr doesn't print raw

Open lhns opened this issue 5 years ago • 5 comments

Describe the bug stderr doesn't print its input as a raw string but as an escaped json string.

To Reproduce

jq -n '"line1\nline2" | stderr | empty'

Outputs "line1\nline2"

Expected behavior As the manual states:

stderr Prints its input in raw and compact mode to stderr with no additional decoration, not even a newline.

I expected the program to output be:

line1
line2

Environment (please complete the following information):

  • OS and Version: Redhat Enterprise Linux 6
  • jq version 1.6

lhns avatar Feb 19 '20 12:02 lhns

This is not a bug, but a feature. Perhaps stderr should output raw when jq is in raw output mode, or perhaps we need a new rawstderr builtin. Or perhaps we can leave this to the I/O work we're doing, and then the user can open /dev/null in raw mode.

nicowilliams avatar Feb 20 '20 20:02 nicowilliams

The documentation is somewhat misleading

stderr Prints its input in raw and compact mode to stderr with no additional decoration, not even a newline.

Would let you believe that stderr behave like --raw-output and --compact-output. "Not even a new line" is where it differs from --compact-output.

Perhaps another word than "raw" could be used here?

ajasmin avatar Dec 16 '20 03:12 ajasmin

Yeah this is a bug IMO -- either the behavior or the documentation should be fixed, because right now they're inconsistent.

vergenzt avatar Nov 09 '21 17:11 vergenzt

I am really enjoying jq - unfortunately I tripped up on this as well

I perused the docs quite extensively and it seems there is no way to print an arbitrary string (with escape sequences) to standard error?

I can't use stderr because newlines of its input are automatically escaped. halt_error prints the way I want, but it halts the program. The only way I have been able to print a newline to stderr is through debug - its an unfortunate unaesthetic workaround, but it works.

I don't think that the behavior of stderr should change based on if jq is in raw mode. If it is not possible in one way or another to change stderr, I would welcome another function that yields the expected behavior.

hyperupcall avatar Apr 13 '22 20:04 hyperupcall

Just wanted to register a +1 on folks who have run into this surprising behavior.

danfuzz avatar Jun 21 '22 23:06 danfuzz

+1 from me, too. I was really hoping to provide a customized warning, but since I cannot get stderr to actually create a newline, I guess I'm stuck with debug.

rjbell4 avatar Nov 16 '22 23:11 rjbell4

FWIW, I ended up here because I was trying to do something like this:

. as $line | try fromjson catch $line | stderr

I'm filtering ndjson logs and wanted to be able to use the try fromjson to filter valid json log lines that I could then pipe into further transforms, and wanted to be able to just redirect malformed lines to stderr

jasonk avatar Jan 25 '23 18:01 jasonk