research Question

Linux - Bash Script - Create index with leading zeros * Efficiently *

Avatar of Alan
AlanFlag for New Zealand asked on
Linux* Bash
2 Comments1 Solution12 ViewsLast Modified:
Hi All,

I am trying to write a bash script, and I am stuck trying to work out how to give an index leading zeros without slowing everything down too much.

In my script I have an index ($I) which is incrementing, and I am naming backup files with it thus:

zip "Backup-$I.zip" Path/To/Files
At the risk of being pedantic (never!), my problem is that I would like to always have the filenames use three digits, it just looks kind of, well, untidy otherwise:

001
002
...
099
100
101

I figured I would add another variable to take the current value of $I, convert it to "000" format, and then use that variable to name the zip file something like this:

Padded=$(printf "%03d" $I)

zip "Backup-$Padded.zip" Path/To/Files/
However, this seems to slow things to a crawl.  I'm not sure why, but I am guessing that it is creating a new shell instance each time it goes through or something?

Is there a better / more efficient option?

Thanks,

Alan.

ASKER CERTIFIED SOLUTION
simon3270

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros