Remember zero padding when producing numerical codes like bar codes

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the algorithms category.

Last Updated: 2024-11-23

What went wrong with this code with respect to producing 12 digit bar codes?

for i in $(seq 0 60); do echo 06634500"$i"01; done | pbcopy

Answer: The first ten entries had only 11 digits instead of 12 digits. This is because the first ten sequence entries had 1 digit (e.g. 0, 1, 2 etc.) and were not zero padded (e.g. 00, 01, 02, etc.)

The fix (in bash) is seq -w

for i in $(seq -w 0 60); do echo 06634500"$i"01; done | pbcopy