Differences between revisions 2 and 24 (spanning 22 versions)
Revision 2 as of 2007-07-03 15:08:19
Size: 2524
Editor: GreyCat
Comment: clean up
Revision 24 as of 2014-05-22 02:20:17
Size: 6154
Editor: ormaaj
Comment: Work on examples
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq18)]] <<Anchor(faq18)>>
Line 5: Line 5:
Bash version 4 allows zero-padding and ranges in its BraceExpansion:

{{{
    # Bash 4 / zsh
    for i in {01..10}; do
        ...
}}}

All of the other solutions on this page will assume Bash earlier than 4.0, or a non-Bash shell.
Line 7: Line 17:
    # Bash / ksh / zsh
Line 9: Line 20:
        echo $i         echo "$i"
Line 13: Line 24:
Output:
{{{
   00
   01
   02
   03
   [...]
}}}

This gets tedious for large sequences, but there are other ways, too.
If you have the {{{printf}}} command (which is a Bash builtin, and is also POSIX standard), it can be used to format a number:
In Bash 3, you can use ranges inside brace expansion (but not zero-padding). Thus, the same thing can be accomplished more concisely like this:
Line 26: Line 27:
    for ((i=1; i<=10; i++)) # Bash 2 for-loop syntax     # Bash 3
    for i in 0{1..9} 10
Line 28: Line 30:
        printf "%02d " "$i"         echo "$i"
Line 32: Line 34:
In Bash 3, you can use ranges inside brace expansion.
Also, since {{{printf}}} will implicitly loop if given more arguments than format specifiers, you can simplify this enormously:
Another example, for output of 0000 to 0034:
Line 36: Line 37:
   printf "%03d\n" {1..300} # Bash 3 brace expansion     # Bash 3
    for i in {000{0..9},00{10..34}}
    do
        echo "$i"
    done

    # using the outer brace instead of just adding them one next to the other
    # allows to use the expansion, for instance, like this:
    wget 'http://foo.com/adir/thepages'{000{0..9},00{10..34}}'.html'
Line 39: Line 48:
The KornShell and KornShell93 have the {{{typeset}}} command to specify the number of leading zeros: This gets tedious for large sequences, but there are other ways, too. If you have the {{{printf}}} command (which is a Bash builtin, and is also POSIX standard), it can be used to format a number:
{{{
    # Bash / ksh93 / zsh
    for ((i = 1; i <= 10; i++)); do
        i=$(printf %02d "$i")
        ...
    done
}}}

Also, unlike the C library `printf`, since {{{printf}}} will implicitly loop if given more arguments than format specifiers, you can simplify this enormously:
{{{
   # Bash 3
   printf '%03d\n' {1..300}
}}}

If you don't know in advance what the starting and ending values are:
{{{
   # Bash 3
   # start and end are variables containing integers
   eval "printf '%03d\n' {$start..$end}"
}}}

The `eval` is required in Bash because for each command it performs an initial pass of evaluation, going through each word to process brace expansions prior to any other evaluation step. The traditional Csh implementation, which all other applicable shells follow, insert the brace expansion pass sometime between the processing of other expansions and pathname expansion, thus parameter expansion has already been performed by the time words are scanned for brace expansion. There are various pros and cons to Bash's implementation, this being probably the most frequently cited drawback. Given how messy that `eval` solution is, please give serious thought to using a `for` or `while` loop with shell arithmetic instead.

The ksh93 method for specifying field width for sequence expansion is to add a (limited) `printf` format string to the syntax, which is used to format each expanded word. This is somewhat more powerful, but unfortunately incompatible with bash, and ksh does not understand Bash's field padding scheme:
Line 42: Line 75:
    #ksh93
    echo {0..10..2%02d}
}}}

ksh93 also has a variable attribute that specifies a field with to pad with leading zeros whenever the variable is referenced. The concept is similar to other attributes supported by Bash such as case modification. Note that ksh never interprets octal literals.

{{{
    # ksh93 / mksh / zsh
Line 48: Line 89:
Line 54: Line 94:
Line 60: Line 99:
Line 62: Line 100:
   # POSIX shell, GNU utilities
Line 65: Line 104:
(That may be helpful if your version of {{{seq(1)}}} lacks {{{printf}}}-style format specifiers. Since it's a nonstandard external tool, it's good to keep your options open.) (That may be helpful if you are not using Bash, but you have `seq(1)`, and your version of {{{seq(1)}}} lacks {{{printf}}}-style format specifiers. That's a pretty odd set of restrictions, but I suppose it's theoretically possible. Since `seq` is a nonstandard external tool, it's good to keep your options open.)
Line 67: Line 106:
Finally, the following example works with any BourneShell derived shell to zero-pad each line to three bytes: Be warned however that using `seq` might be considered bad style; it's even mentioned in [[BashGuide/Practices#Don.27t_Ever_Do_These|Don't Ever Do These]].
Line 69: Line 108:
Some BSD-derived systems have `jot(1)` instead of `seq(1)`. In accordance with the glorious tradition of Unix, it has a completely incompatible syntax:
Line 70: Line 110:
   # POSIX shell, OpenBSD et al.
   printf "%02d\n" $(jot 10 1)

   # Bourne shell, OpenBSD (at least)
   jot -w %02d 10 1
}}}

Finally, the following example works with any BourneShell derived shell (which also has `expr` and `sed`) to zero-pad each line to three bytes:
{{{
   # Bourne
Line 81: Line 131:
Now, since the number one reason this question is asked is for downloading images in bulk, you can use the {{{printf}}} command with {{{xargs(1)}}} and {{{wget(1)}}} to fetch files: But if you're going to rely on an external Unix command, you might as well just do the whole thing in `awk` in the first place:
{{{
   # Bourne
   # count variable contains an integer
   awk -v count="$count" 'BEGIN {for (i=1;i<=count;i++) {printf("%03d\n",i)} }'
Line 83: Line 137:
{{{
   printf "%03d\n" {$START..$END} | xargs -i% wget $LOCATION/%
   # Bourne, with Solaris's decrepit and useless awk:
   awk "BEGIN {for (i=1;i<=$count;i++) {printf(\"%03d\\n\",i)} }"
Line 87: Line 141:
Or, in a slightly more general case: ----
Line 89: Line 143:
Now, since the number one reason this question is asked is for downloading images in bulk, you can use the examples above with {{{xargs(1)}}} and {{{wget(1)}}} to fetch files:
Line 90: Line 145:
   almost any example above | xargs -i% wget $LOCATION/%
}}}

The `xargs -i%` will read a line of input at a time, and replace the `%` at the end of the command with the input.

Or, a simpler example using a `for` loop:
{{{
   # Bash 3
Line 92: Line 155:
      # other commands       sleep 5
Line 95: Line 158:

Or, avoiding the subshells (requires bash 3.1):
{{{
   # Bash 3.1
   for i in {1..100}; do
      printf -v n %03d $i
      wget "$prefix$n.jpg"
      sleep 5
   done
}}}

----
CategoryShell

How can I use numbers with leading zeros in a loop, e.g. 01, 02?

As always, there are different ways to solve the problem, each with its own advantages and disadvantages.

Bash version 4 allows zero-padding and ranges in its BraceExpansion:

    # Bash 4 / zsh
    for i in {01..10}; do
        ...

All of the other solutions on this page will assume Bash earlier than 4.0, or a non-Bash shell.

If there are not many numbers, BraceExpansion can be used:

    # Bash / ksh / zsh
    for i in 0{1,2,3,4,5,6,7,8,9} 10
    do
        echo "$i"
    done

In Bash 3, you can use ranges inside brace expansion (but not zero-padding). Thus, the same thing can be accomplished more concisely like this:

    # Bash 3
    for i in 0{1..9} 10
    do
        echo "$i"
    done

Another example, for output of 0000 to 0034:

    # Bash 3
    for i in {000{0..9},00{10..34}}
    do
        echo "$i"
    done

    # using the outer brace instead of just adding them one next to the other
    # allows to use the expansion, for instance, like this:
    wget 'http://foo.com/adir/thepages'{000{0..9},00{10..34}}'.html'

This gets tedious for large sequences, but there are other ways, too. If you have the printf command (which is a Bash builtin, and is also POSIX standard), it can be used to format a number:

    # Bash / ksh93 / zsh
    for ((i = 1; i <= 10; i++)); do
        i=$(printf %02d "$i")
        ...
    done

Also, unlike the C library printf, since printf will implicitly loop if given more arguments than format specifiers, you can simplify this enormously:

   # Bash 3
   printf '%03d\n' {1..300}

If you don't know in advance what the starting and ending values are:

   # Bash 3
   # start and end are variables containing integers
   eval "printf '%03d\n' {$start..$end}"

The eval is required in Bash because for each command it performs an initial pass of evaluation, going through each word to process brace expansions prior to any other evaluation step. The traditional Csh implementation, which all other applicable shells follow, insert the brace expansion pass sometime between the processing of other expansions and pathname expansion, thus parameter expansion has already been performed by the time words are scanned for brace expansion. There are various pros and cons to Bash's implementation, this being probably the most frequently cited drawback. Given how messy that eval solution is, please give serious thought to using a for or while loop with shell arithmetic instead.

The ksh93 method for specifying field width for sequence expansion is to add a (limited) printf format string to the syntax, which is used to format each expanded word. This is somewhat more powerful, but unfortunately incompatible with bash, and ksh does not understand Bash's field padding scheme:

    #ksh93
    echo {0..10..2%02d}

ksh93 also has a variable attribute that specifies a field with to pad with leading zeros whenever the variable is referenced. The concept is similar to other attributes supported by Bash such as case modification. Note that ksh never interprets octal literals.

    # ksh93 / mksh / zsh
    $ typeset -Z3 i=4
    $ echo $i
    004

If the command seq(1) is available (it's part of GNU sh-utils/coreutils), you can use it as follows:

    seq -w 1 10

or, for arbitrary numbers of leading zeros (here: 3):

    seq -f "%03g" 1 10

Combining printf with seq(1), you can do things like this:

   # POSIX shell, GNU utilities
   printf "%03d\n" $(seq 300)

(That may be helpful if you are not using Bash, but you have seq(1), and your version of seq(1) lacks printf-style format specifiers. That's a pretty odd set of restrictions, but I suppose it's theoretically possible. Since seq is a nonstandard external tool, it's good to keep your options open.)

Be warned however that using seq might be considered bad style; it's even mentioned in Don't Ever Do These.

Some BSD-derived systems have jot(1) instead of seq(1). In accordance with the glorious tradition of Unix, it has a completely incompatible syntax:

   # POSIX shell, OpenBSD et al.
   printf "%02d\n" $(jot 10 1)

   # Bourne shell, OpenBSD (at least)
   jot -w %02d 10 1

Finally, the following example works with any BourneShell derived shell (which also has expr and sed) to zero-pad each line to three bytes:

   # Bourne
   i=0
   while test $i -le 10
   do
       echo "00$i"
       i=`expr $i + 1`
   done |
       sed 's/.*\(...\)$/\1/g'

In this example, the number of '.' inside the parentheses in the sed command determines how many total bytes from the echo command (at the end of each line) will be kept and printed.

But if you're going to rely on an external Unix command, you might as well just do the whole thing in awk in the first place:

   # Bourne
   # count variable contains an integer
   awk -v count="$count" 'BEGIN {for (i=1;i<=count;i++) {printf("%03d\n",i)} }'

   # Bourne, with Solaris's decrepit and useless awk:
   awk "BEGIN {for (i=1;i<=$count;i++) {printf(\"%03d\\n\",i)} }"


Now, since the number one reason this question is asked is for downloading images in bulk, you can use the examples above with xargs(1) and wget(1) to fetch files:

   almost any example above | xargs -i% wget $LOCATION/%

The xargs -i% will read a line of input at a time, and replace the % at the end of the command with the input.

Or, a simpler example using a for loop:

   # Bash 3
   for i in {1..100}; do
      wget "$prefix$(printf %03d $i).jpg"
      sleep 5
   done

Or, avoiding the subshells (requires bash 3.1):

   # Bash 3.1
   for i in {1..100}; do
      printf -v n %03d $i
      wget "$prefix$n.jpg"
      sleep 5
   done


CategoryShell

BashFAQ/018 (last edited 2019-08-21 16:24:29 by GreyCat)