How to zero-pad numeric variables in zsh (and maybe also bash?) How to zero-pad numeric variables in zsh (and maybe also bash?) linux linux

How to zero-pad numeric variables in zsh (and maybe also bash?)


For reference sake, if you do not have control over the generation of the numeric values, you can do padding with:

% value=314% echo ${(l:10::0:)value}0000000314% echo $value314


Use the -w flag to seq (in any shell):

$ seq -w 1 1001020304050607080910


You can use bash's brace expansion:

$ for n in file-{0001..1000}.txt; do echo $n; donefile-0001.txtfile-0002.txtfile-0003.txtfile-0004.txtfile-0005.txtfile-0006.txtfile-0007.txtfile-0008.txtfile-0009.txtfile-0010.txt...file-0998.txtfile-0999.txtfile-1000.txt

Works in zsh too.