Range with leading zero in bash

前端 未结 5 1518
慢半拍i
慢半拍i 2020-12-31 03:06

How to add leading zero to bash range?
For example, I need cycle 01,02,03,..,29,30
How can I implement this using bash?

相关标签:
5条回答
  • 2020-12-31 03:29

    You can use seq's format option:

    seq -f "%02g" 30
    
    0 讨论(0)
  • 2020-12-31 03:39

    A "pure bash" way would be something like this:

    echo {0..2}{0..9}
    

    This will give you the following:

    00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    

    Removing the first 00 and adding the last 30 is not too hard!

    0 讨论(0)
  • 2020-12-31 03:41

    In recent versions of bash you can do:

    echo {01..30}
    

    Output:

    01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    

    Or if it should be comma separated:

    echo {01..30} | tr ' ' ','
    

    Which can also be accomplished with parameter expansion:

    a=$(echo {01..30})
    echo ${a// /,}
    

    Output:

    01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
    
    0 讨论(0)
  • 2020-12-31 03:42

    This works:

    printf " %02d" $(seq 1 30)
    
    0 讨论(0)
  • 2020-12-31 03:44

    another seq trick will work:

     seq -w 30
    

    if you check the man page, you will see the -w option is exactly for your requirement:

    -w, --equal-width
                  equalize width by padding with leading zeroes
    
    0 讨论(0)
提交回复
热议问题