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
Advertisements

One Comment on “Zero padding for Bash scripts”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s