sh icon indicating copy to clipboard operation
sh copied to clipboard

interp: trace is broken for strings with new lines

Open riacataquian opened this issue 4 years ago • 1 comments

Given echo.sh as the file input:

$ cat echo.sh
#!/bin/bash

set -ex

s="The default format for %v is:
bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p"
echo "$s"

In bash, the trace for the variable and echo expression is printed as echo would:

$ ./test.sh
+ s='The default format for %v is:\n
bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p'
+ echo 'The default format for %v is:\n
bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p'
The default format for %v is:\n
bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p

Gosh's trace does not print the whitespaces properly:

$ ./gosh test.sh
+ s=$'The default format for %v is:\\n\nbool:                    %t\nint, int8 etc.:          %d\nuint, uint8 etc.:        %d, %#x if printed with %#v\nfloat32, complex64, etc: %g\nstring:                  %s\nchan:                    %p\npointer:                 %p'
+ echo $'The default format for %v is:\\n\nbool:                    %t\nint, int8 etc.:          %d\nuint, uint8 etc.:        %d, %#x if printed with %#v\nfloat32, complex64, etc: %g\nstring:                  %s\nchan:                    %p\npointer:                 %p'
The default format for %v is:\n
bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p

riacataquian avatar Nov 05 '21 13:11 riacataquian

This is probably to do with how syntax.Quote deals with newlines. I'm a bit confused, because the current heuristic only reaches for $' if there are any non-printable or invalid characters in the string.

mvdan avatar Nov 08 '21 15:11 mvdan