Range with leading zero in bash

前端 未结 5 1517
慢半拍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: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
    

提交回复
热议问题