mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.99k stars 333 forks source link

set -x breaks piping #926

Closed graf0 closed 1 year ago

graf0 commented 1 year ago

Hello!

When you enable tracer via set -x, it breaks pipes.

how to reproduce?

install jq, then:

#!/bin/gosh
set -x
echo '{"number":1}' | jq '.number'

efect of this script should be:

1

is:

+ jq .number
parse error: Invalid numeric literal at line 1, column 2

it could be even simpler without jq:

#!/bin/gosh
set -x
echo "test" | tee test.txt

in test.txt there is following content:

+ echo test
test

where is problem?

in file interp/trace.go, line 28 I think we should set stderr as output stream. Otherwise printed traces become input of next command in the pipe.

mvdan commented 1 year ago

Nicely spotted - I think you're right:

$ bash -x -c 'echo foo' >/dev/null
+ echo foo
$ bash -x -c 'echo foo' 2>/dev/null
foo

Want to send a PR? cc @riacataquian

graf0 commented 1 year ago

Want to send a PR? cc @riacataquian

Sure - you have it :)