ervingsb
ervingsb

Reputation: 693

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

In zsh, when I have to create a bunch of files with zsh, I usually do something like:

for x in $(seq 1 1000); do .....; done

This works fine, it gives me files with names foo-1.txt .. foo-1000.txt.
However, these files do not sort nicely, so I would like to zero-pad the $x variable, thus producing names of foo-0001.txt .. foo-1000.txt.

How to do that in zsh? (and bonus question, how to do it in bash?)

Upvotes: 36

Views: 8153

Answers (6)

user unknown
user unknown

Reputation: 36229

Works in bash (don't know for zsh):

echo foo{0000..1000}.txt 

Upvotes: 4

Konstantin Nikitin
Konstantin Nikitin

Reputation: 2606

in Zsh:

% my_num=43
% echo ${(l(5)(0))my_num}
00043

Upvotes: 10

user8017719
user8017719

Reputation:

  1. printf
    Using printf to pad with zeros works in any shell:

    $ printf 'foo%03d.txt\n' $(seq 4)
    foo001.txt
    foo002.txt
    foo003.txt
    foo004.txt
    
    $           printf  'foo%03d.txt\n' {1..4}       # works in bash, ksh, zsh.
    $ n=4;      printf  'foo%03d.txt\n' {1..$n}      # only ksh, zsh
    $ n=4; eval printf '"foo%03d.txt\n" {1..'"$n"'}' # workaround needed for bash
    
  2. Use brace expansion (only bash and zsh keep the leading zeros ksh don't).

    $ printf '%s\n' foo{1..004}.txt
    foo001.txt
    foo002.txt
    foo003.txt
    foo004.txt
    

Upvotes: 1

Francisco
Francisco

Reputation: 4110

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 $value
314

Upvotes: 40

glenn jackman
glenn jackman

Reputation: 246744

You can use bash's brace expansion:

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

Works in zsh too.

Upvotes: 13

Mat
Mat

Reputation: 206659

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

$ seq -w 1 10
01
02
03
04
05
06
07
08
09
10

Upvotes: 19

Related Questions