syntax highlight

Thursday 15 September 2011

Zero padding for Bash scripts

Lately I found myself trying to generate a video from a series of images generated by a program. Doesn't sound difficult, until you start running into a stupid issue: your 1000th frame will come before your 2nd frame!

Luckily there's a very easy fix for this problem, just add zero padding in a bash script. How?

for i in `seq 1 10`; do echo $i; done

That will print all the numbers between 1 and 10. This one will do the same, with zero padding:

for i in `seq 1 10`; do printf "%02dn" $i; done

1 comment: