Skip to content

fix(echo): decode \x, \e and \u escapes and write octal escapes as bytes - #350

Open
ferdingler wants to merge 4 commits into
strands-agents:mainfrom
ferdingler:fix/336-echo-escapes
Open

ferdingler wants to merge 4 commits into
strands-agents:mainfrom
ferdingler:fix/336-echo-escapes

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

echo -e didn't understand \x, \e or \u escapes, so things like \e[31m color codes came out as literal text. And an octal escape above \0177 came out as two bytes instead of one, so echo -e '\0303\0251' printed mojibake instead of é. While checking this I found printf had the same gap with \e, plus \a, \b, \f and \v.

Now echo uses the same escape decoder as printf, which writes raw bytes and already knew \x and \u. I added \e and the missing control escapes to that decoder, so printf picks them up too. Echo keeps its own octal rule: only \0NNN is an escape, so \101 still prints literally like in bash. The old separate echo decoder is gone. Plain echo without -e still leaves the new escapes literal, as it did before, so JSON or Windows paths echoed into a file don't change.

I added differential cases for the echo and printf escapes and the byte counts, plus one integration test for \u and \U, because bash in the C locale the differential harness uses prints those literally. On main the three new differential cases and the integration test failed. With the change they pass, and the full test suite passes.

Related Issues

Fixes #336

Documentation PR

N/A

Type of Change

Bug fix

Testing

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@github-actions github-actions Bot added the bug Something isn't working label Nov 21, 2026

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested. echo -e and printf now match bash 5.2 on every case tested, and the new tests prove the fix. One blocker: plain echo without -e also picks up the five new escapes, and that case matched bash on main. Details and two fix options are in the inline comment.

What I verified

Head 4c822b1, base dbb6189, bash 5.2.15, Linux aarch64, debug builds.

  • Suites on head: cargo test --workspace --all-targets exits 0 with 2,734 passed, 0 failed, 0 ignored. cargo fmt --check and cargo clippy --workspace --all-targets are clean (19 warnings on both base and head, none in the changed files). I did not run pytest tests/python or npm test.
  • Tests prove the fix: with the PR's tests/ on base source, the three new differential cases and echo_esc_unicode fail, and nothing else does.
  • .expected files: re-recording the three cases with env -i PATH=/usr/bin:/bin HOME=$PWD LC_ALL=C bash case.sh gives byte-identical output.
  • Issue #336: its four echo -e rows and printf 'A\e[0mB\n' produce bash's bytes.
  • echo -e, printf format and %b: about 370 hand-written cases and 16,500 random escape strings compared with bash. The only mismatches are invalid code points (\uD800, \U110000), which the code comment documents, and gaps that are the same on base.
  • No collateral change: 48,000 random inputs without the new escapes give byte-identical output on base and head.
  • Raw bytes: echo -e '\xff' | wc -c is 2 like bash (5 on base). Pipes, >, >> and tee keep the bytes. 47 error-path cases (closed pipe, >&-, read-only file, pipefail, set -e) give the same status and stderr presence on base and head.
  • Fixed on the way: echo '\0400' panics on a debug build of base (src/builtins/echo.rs:82: attempt to multiply with overflow, exit 101). Head prints the low byte like bash.

Not tested: release builds, non-Linux platforms, wasm, and the Python and Node bindings.

Appendix: non-blocking (4)
  • ⚪ src/builtins/printf.rs:413-420: the three guards are correct in all three modes, but the octal rule is spread over negated tests. One match (mode, c) that returns the digit count would read more directly.
  • ⚪ src/builtins/printf.rs:28: the comment says "printf and echo -e output", but plain echo and echo -E use Out too.
  • ⚪ tests/shell_integration.rs:5466: the heading echo -e escape sequences still sits above a printf test (noted in #211).
  • ⚪ No test pins echo -e '\0400', the panic this PR fixes.
  • Pre-existing, not filed: { printf '\xff\n'; } | wc -c prints 4 where bash prints 2, on base and head. Output of a compound command is captured as a String (src/os.rs:287), so invalid bytes become U+FFFD. echo -e now reaches the same path. Not filed because it is the lossy capture chosen in #231.

Comment thread src/builtins/echo.rs Outdated
expand_escapes(&text)
let mut out = Out::default();
if escapes {
if escape_all(&text, Escapes::Echo, &mut out) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plain echo (no -e) now decodes \x, \e, \E, \u and \U too; on main it printed them literally, like bash

escapes defaults to true (line 17), so a flagless echo goes through this same call. The old decoder had no arms for these five escapes, so they came out untouched. Now text that passes through plain echo is rewritten:

Command bash and main this PR
echo '{"msg":"hi \ud83d\ude00"}' | jq -r .msg hi 😀 hi (surrogate pair dropped, exit 0)
echo '{"msg":"say \u0022hi\u0022"}' | jq -r .msg say "hi" jq parse error, exit 5
echo 'C:\xampp\htdocs' literal C:, a newline, mpp\htdocs
echo 'print("\x1b[31mred\x1b[0m")' literal \x1b raw ESC bytes

Plain echo already expanded \n, \t, \\ and \0NNN before this PR (the echo_backslash_literal known-difference), so this widens a known gap. But JSON \u escapes and \x in source code are ordinary things to echo into a file, and the PR description only mentions echo -e.

Suggestion: default to escapes = false on line 17, as bash does. I tried that on this head: 14 integration tests that pin the default expansion fail (echo_esc_*, echo_escape_*, dquote_backslash_nonspecial), and the differential harness asks to remove the marker from echo_backslash_literal.sh. If that's too big for this PR, decode the five new escapes only when -e was given. Either way, add a plain-echo test with these escapes; none exists, so the suite passes both ways.

Reproduction details

Head 4c822b1, base dbb6189 (main), debug builds, Linux aarch64. Oracle: bash 5.2.15. All run as <shell> -c '<command>' with LC_ALL=C.UTF-8, stdout read with od -c. jq is the host's /usr/bin/jq.

$ echo 'A\x41B\e[0m\u00e9'
bash   A \ x 4 1 B \ e [ 0 m \ u 0 0 e 9 \n
main   A \ x 4 1 B \ e [ 0 m \ u 0 0 e 9 \n
head   A A B 033 [ 0 m 303 251 \n

$ echo '{"msg":"hi \ud83d\ude00"}' | jq -r .msg
bash   h i 360 237 230 200 \n      status 0
main   h i 360 237 230 200 \n      status 0
head   h i \n                      status 0

$ echo '{"msg":"say \u0022hi\u0022"}' | jq -r .msg
bash   s a y " h i " \n            status 0
main   s a y " h i " \n            status 0
head   jq: parse error: expected `,` or `}` at line 1 column 14      status 5

$ echo 'C:\xampp\htdocs'
bash   C : \ x a m p p \ h t d o c s \n
main   C : \ x a m p p \ h t d o c s \n
head   C : \n m p p \ h t d o c s \n

$ echo 'print("\x1b[31mred\x1b[0m")'
bash   p r i n t ( " \ x 1 b [ 3 1 m r e d \ x 1 b [ 0 m " ) \n
main   p r i n t ( " \ x 1 b [ 3 1 m r e d \ x 1 b [ 0 m " ) \n
head   p r i n t ( " 033 [ 3 1 m r e d 033 [ 0 m " ) \n

Control: echo -E 'A\x41\e\u00e9' prints the text literally on bash, main and head.

Wider sweep: echo 'P\<c>41Q' for every printable ASCII <c> differs from bash on main for \0 \\ \a \b \c \f \n \r \t \v; head adds exactly \e \E \x \u \U. Of 49 realistic plain-echo commands (JSON, source code, Windows paths, regexes, LaTeX), 40 matched bash on main and no longer do on head.

The surrogate case loses data because src/builtins/printf.rs:455-459 skips code points that aren't valid chars.

Flip experiment: on head with line 17 changed to let mut escapes = false;, cargo test --workspace --all-targets --no-fail-fast fails only those 14 tests in tests/shell_integration.rs and the differential test, whose message is echo_backslash_literal now matches bash: remove the known-difference marker.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. I took the second option. Plain echo with neither -e nor -E now leaves \x, \u, \U, \e and \E literal again, the way it did on main, while echo -e decodes them. Flipping the default to bash's escapes = false is a bigger behavior change, and it breaks the tests that pin the current default, so I left that out of this PR.

Test first in d1f6ff6: a new differential case runs plain echo with these escapes, a JSON \u0022 string and C:\xampp, plus an echo -E control. It failed on 4c822b1 and passes on 6361915. That commit also pins echo -e 'A\0400B' (4 bytes, like bash).

From the appendix: I fixed the Out comment and replaced the negated octal guard with a direct check for \0 or the printf modes. I left the echo -e heading in shell_integration alone since #211 already tracks it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6361915: the five repros above now match bash and main, and the new differential case fails on 4c822b1 and passes here.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. The blocker from my last review is fixed at 6361915: plain echo leaves \x, \u, \U, \e and \E literal again, and echo -e still decodes them. I found nothing new in the delta.

What I verified

Head 6361915, previous head 4c822b1, base dbb6189, bash 5.2.15, Linux aarch64, debug builds.

  • Previous repros: all five plain-echo commands from the inline comment (JSON surrogate pair, \u0022, C:\xampp\htdocs, \x1b in source, the mixed line) now print the same bytes as bash and main.
  • Plain echo is back to main: 2,956 random escape strings through echo and 984 through echo -n are byte-identical on base and head. I left out \0NNN values above \0177, where base writes two bytes or panics and head writes one.
  • -e and printf are untouched by the delta: 3,000 echo -e, 2,000 printf format and 2,000 %b random strings are byte-identical on 4c822b1 and 6361915. 1,579 echo -e strings without \u/\U match bash.
  • Flag combinations match bash: -n, -nE, -E -e, -e -E, -ne, -n -e, -eE, -Ee, -x, --, a leading empty argument, /bin/echo, command echo, builtin echo.
  • New test proves the fix: echo_plain_keeps_new_escapes_literal matches its .expected on base and 6361915 and fails on 4c822b1. Both touched .expected files are byte-identical to a fresh bash recording under LC_ALL=C.
  • Suites: cargo test --workspace --all-targets exits 0 with 2,734 passed, 0 failed. cargo fmt --check is clean and clippy reports the same 19 warnings as base, none in the changed files. I did not run pytest tests/python or npm test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] echo -e prints \xHH, \e and \uHHHH literally and writes \0NNN bytes above 0177 as Latin-1

2 participants