Repository navigation
fix(capture): strip a CRLF terminator from command-substitution output (niubash #120) - #115
Merged
Merged
Conversation
GNU bash deletes every trailing newline from a substitution's output and keeps any other trailing byte, so on Unix `o=$(printf 'a\r\n')` is "a\r". That holds when the tools in the pipe are GNU tools. niubash runs Windows-native tools whose CRT text mode turns every '\n' into '\r\n' (measured with a 47-probe sweep: gawk, GoAWK's awk, ugrep, python, jq, xz, curl and 7z emit CRLF; winuxcmd's own builtins, rg, fd, bat, node, git, yq and age emit LF), and a native shell reads those pipes without the MSYS text-mode translation that maps "\r\n" back to "\n". The stray '\r' therefore survives into `$o`, where one invisible trailing byte breaks `[ "$o" = x ]`, `case` labels, array keys and concatenated paths (niubash #120). `n=$(echo '{"n":2}' | jq -r .n)` compared unequal to "2" for exactly this reason. Treat a '\r' that immediately precedes a stripped '\n' as part of the terminator; keep a lone trailing '\r' so the GNU-fidelity case stays intact, and leave interior CRLF alone. This is the policy the lexer already applies to CRLF script lines (niubash #106, 3ce1872); the capture side was the part missing it. Sites fixed: - `SubstitutionOutput::readback` (substitution_metadata.rs) is the byte-level choke point for every substitution that goes through the real executor, so it carries the rule for the general path (`$(cmd)`, pipelines, compound bodies). - CaptureTerminator/trim_capture_terminator_bytes (command_subst_helpers.rs) hold the rule once for text and once for bytes. - The 18 specialized capture sites that stripped only '\n' (command_substitution*.rs, embedded_mutations.rs) and the `type -t` shortcut, which stripped every trailing '\r'. - `read -t` dropped the '\r' before the newline of a timed line, unlike non-timed `read`. `read` itself is deliberately unchanged: trim_read_input also drops a lone trailing '\r', which GNU keeps, but that pop is load-bearing because the pty/pipe readers hand over a line without its delimiter. Reversing it only for the reading paths that report the delimiter is a separate change; the comment in trim_read_input records the constraint. Interior CRLF stays untouched, so multi-line captures only differ from before in the terminator. Verified against niubash 1.1.4 (rubash 8b81c75) on 16 single-line captures: 3 pass before, 15 pass after (the two excluded cases are `read`, unchanged).
This was referenced Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes unixwin/niubash#120.
Problem
$( )captures a trailing\rfrom every Windows-native tool:GNU (subst.c
command_substitute) deletes every trailing newline and keeps anyother trailing byte, so on Unix
o=$(printf 'a\r\n')isa\r. That holds whenthe tools in the pipe are GNU tools. Here they are Windows-native, and their CRT
text mode turns every
\ninto\r\n; a native shell reads those pipes withoutthe MSYS text-mode translation that maps
\r\nback to\n, so the\rreaches$oand breaks[ "$o" = x ],caselabels, array keys and concatenated paths.A 47-probe sweep of the shipped tools: CRLF from
gawk, GoAWK'sawk,ugrep,python,jq,xz,curl,7z(all C/C++ builds); LF fromwinuxcmd's own builtins,
rg,fd,bat,node,git,yq,age,tar.The earlier attribution ("GoAWK outputs CRLF, GNU awk outputs LF") is wrong —
swapping in GNU awk changes nothing.
Change
Treat a
\rthat immediately precedes a stripped\nas part of theterminator; keep a lone trailing
\r(GNU fidelity) and leave interior CRLFalone. This is the policy the lexer already applies to CRLF script lines
(#106, 3ce1872); the capture side was the part missing it.
SubstitutionOutput::readback— the byte-level choke point every substitutionthat runs through the real executor passes through, so it covers
$(cmd),pipelines and compound bodies.
CaptureTerminator/trim_capture_terminator_bytescarry the rule once fortext, once for bytes.
'\n'and thetype -tshortcut, which stripped every trailing
'\r'.read -tdropped the\rbefore the newline of a timed line, unlikenon-timed
read.readitself is deliberately left alone:trim_read_inputalso drops a lonetrailing
\rthat GNU keeps, but that pop is load-bearing (the pty/pipe readershand over a line without its delimiter, so a CRLF terminator arrives as a bare
trailing
\r). The comment intrim_read_inputrecords what a proper fix wouldrequire.
Verification
Behavioural, against released niubash 1.1.4 (
rubash 8b81c750) on this machine,same script both sides — 16 single-line captures:
Covers
gawk, GoAWKawk,ugrep -c,python,jq, trailing blank lines,$(<file)andcaton a CRLF file,gawk \| head -1,while readon CRLF, then=$(… \| jq -r .n); [ "$n" = 2 ]agent pattern, and the GNU-fidelity cases(
o=$(printf 'a\r')keeps its CR; interior CRLF survives).New unit test
readback_strips_crlf_terminators_onlycovers terminator /repeated terminator / interior CRLF / lone CR.
cargo test --lib: 25 passed in the touched module.cargo test --test cli_testson this machine: 344 passed / 73 failed on abaseline checkout of
8b81c750, 347 passed / 70 failed on this branch — notest regresses, and three flip to green:
examples::gnu_dirstack_function_definitions_parse_commentsexamples::script_array_to_string_example_joins_arraysexamples::script_kshenv_example_parses_multiline_awk_quoteThe remaining 70 failures are identical to the baseline set and unrelated
(arithmetic diagnostic wording, other
examples::*,process_substitution::*).