bash 中前导零的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13376396/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Range with leading zero in bash
提问by Oleg Razgulyaev
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?
如何将前导零添加到 bash 范围?
例如,我需要循环 01,02,03,..,29,30
我如何使用 bash 实现它?
回答by Thor
In recent versions of bash you can do:
在最新版本的 bash 中,您可以执行以下操作:
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
回答by Kent
another seqtrick will work:
另一个seq技巧将起作用:
 seq -w 30
if you check the man page, you will see the -w option is exactly for your requirement:
如果您查看手册页,您会看到 -w 选项完全符合您的要求:
-w, --equal-width
              equalize width by padding with leading zeroes
回答by P.P
You can use seq's format option:
您可以使用 seq 的格式选项:
seq -f "%02g" 30
回答by Neil Winton
A "pure bash" way would be something like this:
“纯 bash”方式将是这样的:
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!
去掉第一个 00 并添加最后 30 个并不太难!
回答by mouviciel
This works:
这有效:
printf " %02d" $(seq 1 30)

