mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.97k stars 332 forks source link

Brace expansion: zero-padding and capital letters not working #1042

Closed qjcg closed 7 months ago

qjcg commented 7 months ago

Hello,

First of all, thanks for the truly excellent library!

I noticed a couple of brace expansions that don't work with this library, but do work in bash, and wanted to flag them here, since I find them quite useful and believe others may as well:

  1. Sequence expressions don't allow zero-padding
  2. Sequence expressions don't work with capital letters

Examples:

expression bash result library result
test{01..03} test01 test02 test03 test1 test2 test3
test{01..3} test01 test02 test03 test1 test2 test3
test{1..03} test01 test02 test03 test1 test2 test3
test{000..100..50} test000 test050 test100 test0 test50 test100
test{A..C} testA testB testC test{A..C}

Here's what's described in bash(1)/Brace Expansion:

[...]

A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or letters, and incr, an optional increment, is an integer. When integers are supplied, the expression expands to each number between x and y, inclusive. Supplied integers may be prefixed with ‘0’ to force each term to have the same width. When either x or y begins with a zero, the shell attempts to force all generated terms to contain the same number of digits, zero-padding where necessary. When letters are supplied, the expression expands to each character lexicographically between x and y, inclusive, using the default C locale. Note that both x and y must be of the same type (integer or letter). When the increment is supplied, it is used as the difference between each term. The default increment is 1 or -1 as appropriate.

mvdan commented 7 months ago

Hi, thanks for spotting these! They look like minor oversights in our implementation.

qjcg commented 7 months ago

Awesome, thanks for the extremely quick updates @mvdan, hugely appreciated!