Recent Changes - Search:

Basics

Languages

Tools

OS

Resources

PmWiki

pmwiki.org

edit SideBar

ZeroPadding

For file names, it is often quite convenient to pad small numbers with zeros in order to have them all sort nicely. It can be a pain to deal with fig1 fig10 fig 11 fig2 fig3 ... when trying to, say, make an animation. Bash has multiple ways to handle that:

Printf function:

$ i=99
$ printf "%05d\n" $i
00099

You can use the -v flag to store the output in another variable:

$ i=99
$ printf -v j "%05d" $i
$ echo $j
00099

Bash innate handling:

for i in {00001..99999}; do
  echo $i
done
00001
00002
...
09999
...
99999
Edit - History - Print - Recent Changes - Search
Page last modified on June 27, 2018, at 03:05 PM