FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`parse()` does not support bash ANSI-C quoting (`$'...'`) · Issue #32 · ljharb/shell-quote · GitHub

parse() does not support bash ANSI-C quoting ($'...') #32

Description

Summary

shell-quote (reproduced on v1.10.0) does not implement bash's ANSI-C quoting — the $'...' form. This is not an exotic construct: it is exactly what Chrome/Edge DevTools emit from Copy as cURL (bash) (e.g. --data-raw $'...'), so any consumer that uses shell-quote to parse real-world copied shell commands silently gets the wrong arguments.

Current behaviour

The scanner treats the leading $ as the start of a parameter expansion (empty name → emits a literal $), and then treats the rest of the token as a plain single-quoted string. Consequences:

  1. the leading $ of the $' prefix is kept in the output (it should be dropped);
  2. ANSI-C escape sequences are not decoded (\n, \t, \x41, \', …);
  3. \' does not escape the quote — it terminates the string and the remainder is mis-parsed.

Reproduction

const { parse } = require('shell-quote'); // v1.10.0

const cases = [
  '$' + "'" + '{"a":"${field}"}' + "'",
  '$' + "'" + 'line1\\nline2' + "'",
  '$' + "'" + 'a\\\'b' + "'",
  '$' + "'" + 'tab\\there' + "'",
  '$' + "'" + '\\x41' + "'",
  '$' + "'" + 'cost is $5' + "'"
];

cases.forEach((c) => console.log(JSON.stringify(c), '->', JSON.stringify(parse(c)[0])));
input bash output (printf '%s' <input>) shell-quote@1.10.0
$'{"a":"${field}"}' {"a":"${field}"} ${"a":"${field}"} ❌ leading $ kept
$'line1\nline2' line1 ⏎ line2 $line1\nline2 ❌ leading $ + escape not decoded
$'a\'b' a'b $a\b ❌ (quote terminated by \', \ kept)
$'tab\there' tab⇥here $tab\there ❌
$'\x41' A $\x41 ❌
$'cost is $5' cost is $5 $cost is $5 ❌ leading $ kept

Expected behaviour

Per the bash manual, inside $'...':

  • the documented escape sequences are decoded (\n, \t, \', \", \\, \nnn, \xHH, \uHHHH, \UHHHHHHHH, \cx, \a, \b, \e, \E, \f, \r, \v, \?, …);
  • no parameter expansion, command substitution or brace expansion happens — ${field} and $5 stay literal;
  • the $' prefix and the surrounding quotes are removed, and the token is not word-split.

Real-world impact / related

Thanks for maintaining this library!

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL