up count with fix character length

Sometimes it is quite important to have all the time the same length of characters. A small bash script can help and with “printf” the beginning indicated has always the same length.


count=0           #counter equals zero   

# star loop until 9999 is reached
while [ $count -le 9999 ]
        myvar=$(printf "%.5d" "$count") #define the length in %.5d 
        echo $myvar

exit 0

To get tis into a text file safe this scrip e.g. as counter.sh as executable and pipe it into a file

./counter.sh > couterfile.txt


Leave a Reply

Your email address will not be published. Required fields are marked *