Zero-padded numbers in bash brace expansion sequences

The described feature is part of bash 4.0 and higher. This information page is therefore obsolete.

Description

Consider the following example:

wget -x http://some.really.long/url/prefix/{,{000..123}.{html,jpg}}

You would probably expect it to download the following files:

However, this is not what bash (version 3.2) currently does. Instead it strips leading zeros from the numbers you supplied, thus trying to download files like 0.html instead. My patch addresses this issue and causes bash to do what you would intuitively expect.

Many people have pointed out that commands such as seq -f %03.0f 0 123 or printf '%03d ' {0..123} can be used to format a sequence of numbers with leading zeros. To get them included in the above brace expression, however, you need some kind of external looping construct. The syntax with brace sequences is more compact, more intuitive, and integrates better with word splitting.

Some other scripting languages provide sequence constructs that preserve leading zeros. E.g. perl can do this.

Download

You can download a patch and apply it to the bash sources. You will have to compile your bash manually.

Compatibility

The patch changes how bash behaves in certain situations, and therefore can potentially break scripts depending on one behaviour. So far I couldn't come up with any probable scenario where a script would rely on leading zeros getting stripped. I think such a scenario would be very unlikely. Nevertheless, I can make no guarantees, so don't sue me if the patch breaks anything.

References

I've tried to get the bash maintainers interested in this patch, but have failed at first. I got next to no reply at all to the thread I had started for this. Most archives list my postings as three independent threads.

Posting: Initial Repost Discussion
Google Groups 8fd6f6111936c52d
Gmane 10453 10454 10464
Mail Archive 03105 03106 03116
GNU Mail Archives 2007-08:00105 2007-08:00106 2007-09:00007

In reply to another mail, sent directly to Chet Ramey, the maintainer of bash, I was told that he considers a feature like this for the next release of bash. This was 7 January 2008.

Version 4.0 really does contain the requested feature. The implementation closely follows the patch offered above.