I need to loop some values,
for i in $(seq $first $last)
do
does something here
done
For $first and $last, I need it to be of fixed length 5. So if the input is 1, I need to add zeros in front such that it becomes 00001. It loops till 99999 for example, but the length has to be 5.
E.g.: 00002, 00042, 00212, 12312 and so forth.
Any idea on how I can do that?
In your specific case though it’s probably easiest to use the
-fflag toseqto get it to format the numbers as it outputs the list. For example:will produce the following output:
More generally,
bashhasprintfas a built-in so you can pad output with zeroes as follows:You can use the
-vflag to store the output in another variable:Notice that
printfsupports a slightly different format toseqso you need to use%05dinstead of%05g.