Skip to content

fix: echo preserves backslash before unrecognised escape sequences - #1901

Merged
gnodet merged 2 commits into
masterfrom
fix/1863
May 23, 2026
Merged

gnodet merged 2 commits into
masterfrom
fix/1863

Conversation

@gnodet

@gnodet gnodet commented May 22, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix processEscapeSequences default case to preserve the backslash before unrecognised escape characters like \", matching POSIX echo behavior

Fixes #1863

Test plan

  • Added test testEchoEscapeSequencePreservesBackslash verifying echo 'hello \"world\"' outputs hello \"world\" instead of hello "world"

Summary by CodeRabbit

  • Bug Fixes
    • echo now preserves backslashes for unrecognized escape sequences, ensuring literal backslash characters appear in output.
    • Handles multiple consecutive unrecognized escapes correctly (they remain verbatim).
    • Mixed sequences now behave as expected: recognized escapes (e.g., newline, tab) are interpreted while unrecognized/escaped-quote sequences keep their backslashes.

Review Change Stack

…ixes #1863)

The default case in processEscapeSequences was dropping the backslash
for unrecognised \X sequences like \". Now preserves the backslash,
matching POSIX echo behavior.
@coderabbitai

coderabbitai Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1e52f1ae-b161-4566-acd5-118170658f42

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between dadd6ae and 29b04a4.

πŸ“’ Files selected for processing (1)
  • builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java

πŸ“ Walkthrough

Walkthrough

Default handling in processEscapeSequences now appends a literal backslash before unrecognized escape characters. Three unit tests were added to verify preserved escaped quotes, multiple unrecognized escapes, and mixed recognized/unrecognized escapes.

Changes

Echo backslash escape sequence fix

Layer / File(s) Summary
Backslash preservation in escape sequence processing
builtins/src/main/java/org/jline/builtins/PosixCommands.java
The default case in processEscapeSequences now appends the literal backslash ('\\') before unrecognized characters so sequences like \" keep the backslash.
Echo tests for unrecognized and mixed escapes
builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java
Adds tests asserting echo preserves escaped quotes, preserves multiple unrecognized escapes verbatim, and correctly handles mixed recognized (e.g., \n, \t) and unrecognized escapes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit trims a little bug,
Restores the slash, gives tests a hug.
" stays brave, no backslash slain,
Echo sings the string again. πŸ‡

πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title accurately and concisely describes the main change: fixing echo to preserve backslashes before unrecognized escape sequences.
Linked Issues check βœ… Passed The PR directly addresses issue #1863 by modifying processEscapeSequences to preserve backslashes for unrecognized escapes and adding comprehensive test coverage.
Out of Scope Changes check βœ… Passed All changes are directly related to fixing the echo escape sequence handling in PosixCommands and adding corresponding tests.
Docstring Coverage βœ… Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ“ Generate docstrings
  • Create stacked PR
  • Commit on current branch
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/1863

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java (1)

106-113: ⚑ Quick win

Consider adding tests for other unrecognized escape sequences.

While the current test validates the reported issue with \", consider adding tests for other unrecognized escape sequences to ensure comprehensive coverage:

πŸ“‹ Suggested additional test cases
`@Test`
void testEchoMultipleUnrecognizedEscapes() throws Exception {
    // Test multiple different unrecognized escapes
    PosixCommands.echo(context, new String[] {"echo", "\\x\\y\\z"});
    
    String output = out.toString();
    String expected = "\\x\\y\\z" + System.lineSeparator();
    assertEquals(expected, output);
}

`@Test`
void testEchoMixedRecognizedAndUnrecognizedEscapes() throws Exception {
    // Mix recognized (\n, \t) with unrecognized (\")
    PosixCommands.echo(context, new String[] {"echo", "line1\\nline2\\ttab\\\"quote"});
    
    String output = out.toString();
    String expected = "line1\nline2\ttab\\\"quote" + System.lineSeparator();
    assertEquals(expected, output);
}
πŸ€– Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java` around
lines 106 - 113, Add two new unit tests in PosixCommandsTest to cover other
unrecognized escape sequences: create testEchoMultipleUnrecognizedEscapes that
calls PosixCommands.echo(context, new String[] {"echo", "\\x\\y\\z"}) and
asserts output equals "\\x\\y\\z" + System.lineSeparator(), and create
testEchoMixedRecognizedAndUnrecognizedEscapes that calls
PosixCommands.echo(context, new String[] {"echo",
"line1\\nline2\\ttab\\\"quote"}) and asserts output equals
"line1\nline2\ttab\\\"quote" + System.lineSeparator(); ensure both tests use the
same out capture used by existing tests and follow the same throws Exception
signature as testEchoEscapeSequencePreservesBackslash.
πŸ€– Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java`:
- Around line 106-113: Add two new unit tests in PosixCommandsTest to cover
other unrecognized escape sequences: create testEchoMultipleUnrecognizedEscapes
that calls PosixCommands.echo(context, new String[] {"echo", "\\x\\y\\z"}) and
asserts output equals "\\x\\y\\z" + System.lineSeparator(), and create
testEchoMixedRecognizedAndUnrecognizedEscapes that calls
PosixCommands.echo(context, new String[] {"echo",
"line1\\nline2\\ttab\\\"quote"}) and asserts output equals
"line1\nline2\ttab\\\"quote" + System.lineSeparator(); ensure both tests use the
same out capture used by existing tests and follow the same throws Exception
signature as testEchoEscapeSequencePreservesBackslash.

ℹ️ Review info
βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a3c42549-d7da-4df3-b93e-b13823eba232

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 798308f and dadd6ae.

πŸ“’ Files selected for processing (2)
  • builtins/src/main/java/org/jline/builtins/PosixCommands.java
  • builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java

Cover additional edge cases for the backslash-preservation fix:
consecutive unrecognized escapes (\x\y\z) and a mix of recognized
(\n, \t) and unrecognized (\") escapes in the same string.
@sonarqubecloud

Copy link
Copy Markdown

@gnodet
gnodet merged commit 2825bc2 into master May 23, 2026
15 checks passed
@gnodet
gnodet deleted the fix/1863 branch May 23, 2026 05:02
gnodet added a commit that referenced this pull request Jun 16, 2026
…1901)

* fix: echo preserves backslash before unrecognised escape sequences (fixes #1863)

The default case in processEscapeSequences was dropping the backslash
for unrecognised \X sequences like \". Now preserves the backslash,
matching POSIX echo behavior.

* test: add tests for multiple and mixed unrecognized echo escapes

Cover additional edge cases for the backslash-preservation fix:
consecutive unrecognized escapes (\x\y\z) and a mix of recognized
(\n, \t) and unrecognized (\") escapes in the same string.
gnodet added a commit that referenced this pull request Jun 16, 2026
…1901)

* fix: echo preserves backslash before unrecognised escape sequences (fixes #1863)

The default case in processEscapeSequences was dropping the backslash
for unrecognised \X sequences like \". Now preserves the backslash,
matching POSIX echo behavior.

* test: add tests for multiple and mixed unrecognized echo escapes

Cover additional edge cases for the backslash-preservation fix:
consecutive unrecognized escapes (\x\y\z) and a mix of recognized
(\n, \t) and unrecognized (\") escapes in the same string.
gnodet added a commit that referenced this pull request Jun 16, 2026
…1901)

* fix: echo preserves backslash before unrecognised escape sequences (fixes #1863)

The default case in processEscapeSequences was dropping the backslash
for unrecognised \X sequences like \". Now preserves the backslash,
matching POSIX echo behavior.

* test: add tests for multiple and mixed unrecognized echo escapes

Cover additional edge cases for the backslash-preservation fix:
consecutive unrecognized escapes (\x\y\z) and a mix of recognized
(\n, \t) and unrecognized (\") escapes in the same string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

echo strips backslash before unrecognised escape sequences

1 participant