> Using the following snippet of scipt as an example, how do I increment
> my variable without loosing the leading zero?
> MyVar=00
> while [ $MyVar -lt 20]
> do
> echo "MyVar is $MyVar"
> MyVar=`expr MyVar + 1 `
> done
> I would like to see an output of:
> 00
> 01
> 02 ...
> 10
> 11
> 12 ...
> 19
> But what I see is:
> 00
> 1
> 2 ...
> 10
> 11 ...
> 19
> Any suggestions? I am willing to use a different shell and I am open
> to using sed or awk.
In any POSIX shell:
MyVar=0
while [ $MyVar -lt 20 ]
do
printf "MyVar is %02d\n" "$MyVar"
MyVar=$(( $MyVar + 1 ))
done
In awk:
awk 'BEGIN { while (MyVar < 20 ) printf "MyVar is %02d\n", MyVar++; exit }'
In bash version 3:
printf "MyVar is %02d\n" {0..19}
In William Park's extended bash:
printf "MyVar is %s\n" {00..19}
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>