Skip to content

fix(expansion): ${var#./"$other_var"} fails to strip when pattern mixes literals and quoted vars #1161

@chaliy

Description

@chaliy

Summary

Parameter expansion prefix stripping `${var#pattern}` fails when the pattern concatenates a literal string (`./`) with a quoted variable (`"$prefix"`). The pattern is not matched and the string is returned unchanged.

Reproduction

i="./tag_hello.tmp.html"
prefix_tags="tag_"

# Fails — does not strip:
result=${i#./"$prefix_tags"}
echo "$result"
# Expected: hello.tmp.html
# Actual: ./tag_hello.tmp.html (unchanged!)

# Works — literal pattern:
result=${i#./tag_}
echo "$result"
# Expected: hello.tmp.html
# Actual: hello.tmp.html (correct)

# Works — variable-only pattern:
result=${i#./$prefix_tags}
echo "$result"
# Expected: hello.tmp.html
# Actual: hello.tmp.html (correct)

Real bash comparison

All three forms produce `hello.tmp.html` in real bash.

Context

Bashblog uses this in `rebuild_tags()`:

while IFS='\'' read -r i; do
    tagname=${i#./"$prefix_tags"}
    tagname=${tagname%.tmp.html}
    create_html_page "$i" "$prefix_tags$tagname.html" ...
done < <(ls -t ./"$prefix_tags"*.tmp.html)

When stripping fails, `tagname` becomes `./tag_hello` instead of `hello`, producing invalid filenames like `tag_./tag_hello.html`.

Expected behavior

`${var#./"$other"}` should concatenate the literal `./` with the value of `$other` to form the pattern, then strip it from the beginning of `$var`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions