Skip to content

[bug] ANSI-C escapes not decoded in the replacement operand of ${var//pat/$'\n'}: literal backslash-n stored ($'\t' too; $'\xHH' works) #323

Closed

Description

@caomengxuan666

Found by ecosweep3 lane (pure-bash-bible corpus, section 004 'Split a string on a delimiter'; carrier release build of 6106961).

Reproducer (target/probe-out/p3.sh, p9.sh)

s="a,b,c"
r="${s//,/$'\n'}"
printf '%s' "$r" | od -An -c
printf '%s\n' "${r@Q}"
  • GNU bash 5.3.0: a \n b \n c (real newlines); ${r@Q} = $'a\nb\nc'
  • rubash: a \ n b \ n c (literal 2-char backslash-n); ${r@Q} = 'a\nb\nc' (single-quote form — @q rendering of the literal backslashes)

Matrix (p9.sh): ${s//,/$'\t'} also literal; ${s//,/$x} (plain var) fine; ${s//,/$'\x2e'} correctly decoded to ." — the \xHH path unescapes but \t / \n do not. Argument position echo $'\n'` is unaffected; the bug is specific to the replacement operand of the pattern-substitution expansion.

GNU anchor

The replacement word is expanded as a normal word: subst.c:9777 parameter_brace_expand() → the /pat/repl branch expands repl via expand_word_internal-equivalent paths where $'...' was already decoded at read time by parse.y read_token_word's ANSI-C handling ($'${ handling near parse.y:5530 region, parse_y ansi quoting). rubash's text-layer replacement path re-splits the replacement without the ANSI-C unescape step.

Impact

pbb split() emits apples\noranges\npears as ONE line instead of 4 lines; the $'\n'-as-separator idiom is common in real scripts.

Activity

  1. added
    bug
    gnu-baseline
    on Sep 29, 2026
  2. added a commit that references this issue on Sep 29, 2026
  3. caomengxuan666 commented on Sep 29, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by wt14/pbbfix (fb91245, master): push_ansi_c_replacement_span had a hand-rolled decoder recognizing only backslash-xHH, backslash-quote and backslash-backslash - now routed through the canonical lexer decoder decode_ansi_c_quoted (GNU: subst.c:4462 -> ansicstr, strtrans.c:51 full table). 22-form matrix (all standard escapes, octal, control-char, unicode, unknown escapes keep the backslash, quote escapes, double-quote-variable literal preservation, @q round-trip) byte-identical.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-baseline

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions