Saturday, March 13, 2021

Formatting Numerics with Bash

Seem like every couple months I need to re-look up how to perform numeric formatting with bash.  Here's a quick example of how to do it.


$ cat /tmp/go 
#!/bin/bash

for i in `seq 100`; do
  N=$(printf %03d $i)
  echo $N
done

$ ./go
001
002
003
...
097
098
099
100


I find doing this that one is no longer the loneliest number, it's paired with a couple of real zeros, but never lonely.


Cheers.

No comments:

Post a Comment