Skip to content

Bash: Allow quotes inside command substitutions and parameter expansions in strings - #4223

Open
O1sumitkumar wants to merge 1 commit into
PrismJS:v2from
O1sumitkumar:fix/bash-nested-quotes-in-substitution
Open

O1sumitkumar wants to merge 1 commit into
PrismJS:v2from
O1sumitkumar:fix/bash-nested-quotes-in-substitution

Conversation

@O1sumitkumar

Copy link
Copy Markdown

Fixes #4218

Summary

The double-quoted string pattern in src/languages/bash.js only let a command substitution in as \$\([^)]+\), so a " inside the substitution ended the string. One line like

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${HERE}"
echo "done"

was split into a string that stops at the quote before && pwd, then a string that starts at the closing quote of line 1 and runs to the first quote of line 2, and from there on every string was code and every stretch between strings was a string.

In Bash, $(…) and ${…} start a new quoting context, so the string pattern is now built by a small doubleQuotedString(depth) helper that allows nested double-quoted strings inside both, three levels deep (the same "a few levels cover real scripts" approach as the issue and as the nesting in mikrotik.js):

  • a command substitution is $( then an escape, a nested string, a '…' string, balanced parentheses (so $(( (1 + 2) * 3 )) still works) or any other character, then );
  • a parameter expansion is ${ then an escape, a nested string, one level of nested ${…} or any other character, then };
  • each alternative starts with a character the others cannot start with, so a string with a missing closing quote fails in one pass instead of backtracking.

The inside grammar is unchanged, so the tokens inside the string are the same as before.

How did you test this change?

  • Added tests/languages/bash/nested_quotes_in_string.test (the snippet above plus "${name:-"default"}" and "$(echo "a $(echo "b")") c"). It fails on v2 without the change and passes with it.
  • npm ci --ignore-scripts, then npm test (all test:* scripts: components, core, identifiers, languages, patterns incl. the exponential/polynomial backtracking checks, plugins, runner) passes; npx eslint on the changed files and npx tsc are clean.
  • Quick timing check on a 5000x repeated unterminated nested string: 664 ms with the change vs 1260 ms on v2 before it (same order of magnitude, no blow-up).
  • Not run: npm run typecheck:tests, npm run benchmark, and the website build.

This change was prepared with AI assistance (Grok Bot, an AI agent). I reviewed the diff and the test results.

@netlify

netlify Bot commented Dec 2, 2026

Copy link
Copy Markdown

👷 Deploy request for dev-prismjs-com pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 4cdd256

@luckyjakhar

Copy link
Copy Markdown

Thanks for working on this. I found another valid Bash case: echo "$( (printf '%s' "inner") )". The proposed parenthesis pattern excludes quotes, and a check of the PR’s pattern matched only the inner "inner" segment—not the full outer string.

I added this case alongside the issue’s example and nested parameter/command substitutions, and adjusted the parenthesis matcher to handle quoted strings. In my local v2 checkout, npm test, npm run lint:ci, and npm run typecheck all pass, including the regex safety tests.

Would you prefer to incorporate this regression test and fix into #4223, or should I open a focused follow-up PR?

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bash: a double-quoted string ends at a quote inside $(...), and every string after it turns inside out

2 participants