6464_QUOTED_GLOB_SENTINEL = "\ue000 "
6565_DYNAMIC_SHELL_WORD_SENTINEL = "\ue001 "
6666_RUNTIME_SHELL_PARAMETER_SENTINEL = "\ue002 "
67- _SIMPLE_BRACED_PARAMETER_RE = re .compile (r"\$\{(?:[A-Za-z_][A-Za-z0-9_]*|[0-9]+|[@*#?$!-])\}" )
67+ # A braced expansion head whose result depends only on parameter values: an
68+ # optional length prefix, a name with an optional literal subscript or a
69+ # numeric/special parameter, then the closing brace or a value operator.
70+ # Indirection, arithmetic subscripts, substring offsets, ``@`` transformations,
71+ # zsh flags and ``${ cmd; }`` can evaluate code, so they do not match. The
72+ # documentation placeholder ``${...}`` is a bad substitution in every shell.
73+ _VALUE_PARAMETER_EXPANSION_HEAD_RE = re .compile (
74+ r"\$\{(?:#?(?:[A-Za-z_][A-Za-z0-9_]*(?:\[(?:[@*]|[0-9]+)\])?|[0-9]+|[@*#?$!-])"
75+ r"(?:\}|:?[-=?+]|##?|%%?|/[/#%]?|\^\^?|,,?)|\.\.\.\})"
76+ )
77+ _VALUE_PARAMETER_EXPANSION_START_RE = re .compile (r"\$\{" )
78+ _EXECUTABLE_EXPANSION_MARKERS = ("`" , "$(" , "$[" , "<(" , ">(" )
79+ _SHELL_ASSIGNMENT_PREFIX_RE = re .compile (r"[A-Za-z_][A-Za-z0-9_]*\+?=" )
80+ _SHELL_ASSIGNMENT_NAME_CHARACTERS = frozenset (
81+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
82+ )
83+ _SHELL_CLAUSE_BOUNDARY_CHARACTERS = "\r \n ;|&(){}`"
6884_SHELL_DELIMITER_WORD_RE = re .compile (r"[^$'\"`\\(){}<>#;|&!\s]++" )
6985# A ``#`` that starts a word begins a shell comment. Quoting is not tracked, so
7086# ``echo "a # b"`` also matches; callers only use this to restrict ownership.
93109_SHELL_COMMAND_STRING_SHELLS = frozenset ({"sh" , "ash" , "bash" , "dash" , "ksh" , "yash" , "zsh" })
94110_SHELL_COMMAND_STRING_ARGUMENTS = 32
95111_SHELL_CLAUSE_PREFIX_WORDS = frozenset ({"do" , "else" , "elif" , "then" , "time" , "!" })
112+ _SHELL_ASSIGNMENT_PREFIX_WORDS = _SHELL_CLAUSE_PREFIX_WORDS | frozenset (
113+ {"if" , "while" , "until" , "export" , "local" , "declare" , "readonly" , "typeset" }
114+ )
96115_SHELL_REDIRECTION_PREFIX_RE = re .compile (r"(?:[0-9]+)?(?:&>>?|<>|>>?|<<-?|>&|<&|[<>])" )
97116_RECURSIVE_OPTION_SOURCE_RE = re .compile (r"-(?:[A-Za-z]*[rR]|-recursive)" )
98117_FORCE_OPTION_SOURCE_RE = re .compile (r"-(?:[A-Za-z]*f|-force)" )
@@ -935,14 +954,27 @@ def next_word() -> str | None:
935954 return True , arguments
936955
937956
957+ def _is_value_parameter_expansion (content : str , start : int , end : int ) -> bool :
958+ """Return whether a braced expansion and its nested expansions substitute only values."""
959+ expansion = content [start :end ]
960+ if any (marker in expansion for marker in _EXECUTABLE_EXPANSION_MARKERS ):
961+ return False
962+ return all (
963+ _VALUE_PARAMETER_EXPANSION_HEAD_RE .match (expansion , nested .start ()) is not None
964+ for nested in _VALUE_PARAMETER_EXPANSION_START_RE .finditer (expansion )
965+ )
966+
967+
938968def _invocation_expansion_marker (content : str , start : int , end : int ) -> str :
939969 """Distinguish runtime-only parameters from possible command reconstruction."""
940970 if content .startswith ("$(" , start ) or (
941- content .startswith ("${" , start )
942- and _SIMPLE_BRACED_PARAMETER_RE .fullmatch (content , start , end ) is None
971+ content .startswith ("${" , start ) and not _is_value_parameter_expansion (content , start , end )
943972 ):
944- # Complex parameter expansions may contain nested command substitutions.
973+ # Other parameter expansions may contain nested command substitutions
974+ # or evaluate a value as code.
945975 return _DYNAMIC_SHELL_WORD_SENTINEL
976+ # ``${NAME:-default}``, ``${ARR[@]}`` and similar forms select the same
977+ # runtime value as ``$NAME`` and need the same printf invocation evidence.
946978 return _RUNTIME_SHELL_PARAMETER_SENTINEL
947979
948980
@@ -1416,6 +1448,50 @@ def _skip_backtick_substitution(
14161448 )
14171449
14181450
1451+ def _previous_word_end (content : str , start : int ) -> int | None :
1452+ """Return where the previous word in this clause ends, or ``None`` at a clause start."""
1453+ cursor = start
1454+ while cursor > 0 and content [cursor - 1 ] in " \t " :
1455+ cursor -= 1
1456+ if cursor == 0 or content [cursor - 1 ] in _SHELL_CLAUSE_BOUNDARY_CHARACTERS :
1457+ return None
1458+ return cursor
1459+
1460+
1461+ def _is_assignment_word (content : str , start : int ) -> bool :
1462+ """Return whether ``start`` begins a prefix assignment word or its quoted value."""
1463+ word_start = start
1464+ # The exhaustion sweep already skips the checks for a quote after ``=``.
1465+ # Recognizing it here keeps the parser consistent for any caller and avoids
1466+ # evaluating printf reconstruction whose result would be discarded.
1467+ if 0 < start < len (content ) and content [start ] in "'\" " and content [start - 1 ] == "=" :
1468+ word_start = start - 1
1469+ if word_start > 0 and content [word_start - 1 ] == "+" :
1470+ word_start -= 1
1471+ while word_start > 0 and content [word_start - 1 ] in _SHELL_ASSIGNMENT_NAME_CHARACTERS :
1472+ word_start -= 1
1473+ prefix = _SHELL_ASSIGNMENT_PREFIX_RE .match (content , word_start )
1474+ if prefix is None or (word_start != start and prefix .end () != start ):
1475+ return False
1476+ # The shell recognizes assignments only before the command name. After
1477+ # ``alias``, ``echo`` or a path prefix, ``NAME=value`` is an ordinary word.
1478+ previous_end = _previous_word_end (content , word_start )
1479+ if previous_end is None :
1480+ return True
1481+ if previous_end == word_start :
1482+ return False
1483+ previous_start = previous_end
1484+ while (
1485+ previous_start > 0
1486+ and content [previous_start - 1 ] not in " \t " + _SHELL_CLAUSE_BOUNDARY_CHARACTERS
1487+ ):
1488+ previous_start -= 1
1489+ return (
1490+ content [previous_start :previous_end ] in _SHELL_ASSIGNMENT_PREFIX_WORDS
1491+ and _previous_word_end (content , previous_start ) is None
1492+ )
1493+
1494+
14191495def _parse_shell_command_word (
14201496 content : str ,
14211497 start : int ,
@@ -1428,6 +1504,23 @@ def _parse_shell_command_word(
14281504 enclosing_delimiter : str | None = None ,
14291505) -> _ShellCommandWord | None :
14301506 runtime_check = check_runtime or (lambda : None )
1507+ # The shell recognizes ``NAME=value`` before expansion and never runs the
1508+ # value as the command name. Its substitutions keep their own candidate
1509+ # positions, and a later ``$NAME`` command word is still checked, so only
1510+ # this word's printf reconstruction check is skipped.
1511+ assignment = _is_assignment_word (content , start )
1512+
1513+ def reconstructs_command (
1514+ substitution_start : int , substitution_end : int , * , backtick : bool = False
1515+ ) -> bool :
1516+ return not assignment and _is_printf_substitution (
1517+ content ,
1518+ substitution_start ,
1519+ substitution_end ,
1520+ backtick = backtick ,
1521+ check_runtime = runtime_check ,
1522+ )
1523+
14311524 output : list [str ] = []
14321525 wrapper_quote = enclosing_delimiter or (
14331526 _command_wrapper_quote (content , start ) if content [start ] in "$`" else None
@@ -1481,12 +1574,7 @@ def _parse_shell_command_word(
14811574 if static_value is None :
14821575 output .append ("$()" )
14831576 dynamic = True
1484- limited = limited or _is_printf_substitution (
1485- content ,
1486- cursor ,
1487- substitution_end ,
1488- check_runtime = runtime_check ,
1489- )
1577+ limited = limited or reconstructs_command (cursor , substitution_end )
14901578 else :
14911579 output .append (static_value )
14921580 cursor = substitution_end
@@ -1531,12 +1619,8 @@ def _parse_shell_command_word(
15311619 if static_value is None :
15321620 output .append ("$()" )
15331621 dynamic = True
1534- limited = limited or _is_printf_substitution (
1535- content ,
1536- cursor ,
1537- substitution_end ,
1538- backtick = True ,
1539- check_runtime = runtime_check ,
1622+ limited = limited or reconstructs_command (
1623+ cursor , substitution_end , backtick = True
15401624 )
15411625 else :
15421626 output .append (static_value )
@@ -1579,12 +1663,7 @@ def _parse_shell_command_word(
15791663 if static_value is None :
15801664 output .append ("$()" )
15811665 dynamic = True
1582- limited = limited or _is_printf_substitution (
1583- content ,
1584- cursor ,
1585- substitution_end ,
1586- check_runtime = runtime_check ,
1587- )
1666+ limited = limited or reconstructs_command (cursor , substitution_end )
15881667 else :
15891668 output .append (static_value )
15901669 cursor = substitution_end
@@ -1633,13 +1712,7 @@ def _parse_shell_command_word(
16331712 if static_value is None :
16341713 output .append ("$()" )
16351714 dynamic = True
1636- limited = limited or _is_printf_substitution (
1637- content ,
1638- cursor ,
1639- substitution_end ,
1640- backtick = True ,
1641- check_runtime = runtime_check ,
1642- )
1715+ limited = limited or reconstructs_command (cursor , substitution_end , backtick = True )
16431716 else :
16441717 output .append (static_value )
16451718 cursor = substitution_end
0 commit comments