bash 如何在shell中为for循环添加前导零?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18460123/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:06:44  来源:igfitidea点击:

How to add leading zeros for for-loop in shell?

bashfor-loop

提问by Bruce Blacklaws

I have a basic number for loop which increments the variable num by 1 over each iteration...

我有一个基本数字 for 循环,它在每次迭代中将变量 num 增加 1 ...

for (( num=1; num<=5; num++ ))
do
 echo $num
done

Which outputs:

哪些输出:

1
2
3
4
5

I'm trying to make it produce the output (add leading zero before $num):

我试图让它产生输出(在 $num 之前添加前导零):

01
02
03
04
05

Without doing:

不做:

echo 0$num

回答by Adrian Frühwirth

Use the following syntax:

使用以下语法:

$ for i in {01..05}; do echo "$i"; done
01
02
03
04
05

If you want to use printf, nothing prevents you from putting its result in a variable for further use:

如果您想使用printf,没有什么可以阻止您将其结果放入变量中以供进一步使用:

$ foo=$(printf "%02d" 5)
$ echo "${foo}"
05

Disclaimer: This only works in >=bash-4.

免责声明:这仅适用于>=bash-4.

回答by piojo

seq -wwill detect width and normalize it.

seq -w将检测宽度并将其标准化。

for num in $(seq -w 01 05); do
    ...
done

Apparently this doesn't work on the newest versions of iOS, so you can either install macports and use its version of seq, or you can set the format explicitly:

显然这不适用于最新版本的 iOS,因此您可以安装 macports 并使用其版本的seq,或者您可以明确设置格式:

seq -f '%02g' 1 3
    01
    02
    03

But given the ugliness of format specifications for such a simple problem, I prefer the solution Henk and Adrian gave, which just uses Bash. Apple can't screw this up since there's no generic "unix" version of Bash:

但是考虑到这样一个简单问题的格式规范的丑陋,我更喜欢 Henk 和 Adrian 给出的解决方案,它只使用 Bash。Apple 不能搞砸,因为没有通用的“unix”版本的 Bash:

echo {01..05}

Or:

或者:

for number in {01..05}; do ...; done

回答by anubhava

Use printfcommand to have 0padding:

使用printf命令来0填充:

printf "%02d\n" $num

Your for loop will be like this:

您的 for 循环将是这样的:

for (( num=1; num<=5; num++ )); do printf "%02d\n" $num; done
01
02
03
04
05

回答by David W.

I'm not interested in outputting it to the screen (that's what printf is mainly used for, right?) The variable $num is going to be used as a parameter for another program but let me see what I can do with this.

我对将它输出到屏幕不感兴趣(这就是 printf 的主要用途,对吗?)变量 $num 将用作另一个程序的参数,但让我看看我可以用它做什么。

You can still use printf:

您仍然可以使用printf

for num in {1..5}
do
   value=$(printf "%02d" $num)
   ... Use $value for your purposes
done

回答by Henk Langeveld

From bash 4.0 onward, you can use Brace Expansionwith fixed length strings. See below for the original announcement.

从 bash 4.0 开始,您可以将Brace Expansion与固定长度的字符串一起使用。请参阅下面的原始公告。

It will do just what you need, and does not require anything external to the shell.

它会做你所需要的,不需要外壳外部的任何东西。

$ echo {01..05}
01 02 03 04 05

for num in {01..05}
do
  echo $num
done
01
02
03
04
05


CHANGES, release bash-4.0, section 3

更改,发布 bash-4.0,第 3 节

This is a terse description of the new features added to bash-4.0 since the release of bash-3.2.

. . .

z. Brace expansion now allows zero-padding of expanded numeric values and will add the proper number of zeroes to make sure all values contain the same number of digits.

这是自 bash-3.2 发布以来添加到 bash-4.0 的新特性的简要描述。

. . .

z。大括号扩展现在允许对扩展数值进行零填充,并将添加适当数量的零以确保所有值包含相同数量的数字。

回答by Benoit

why not printf '%02d' $num? See help printffor this internal bash command.

为什么不printf '%02d' $num呢?请参阅help printf此内部 bash 命令。

回答by jd_doubley

Just a note: I have experienced different behaviours on different versions of bash:

请注意:我在不同版本的 bash 上经历了不同的行为:

  • version 3.1.17(1)-release-(x86_64-suse-linux) and
  • Version 4.1.17(9)-release (x86_64-unknown-cygwin))
  • 版本 3.1.17(1)-release-(x86_64-suse-linux) 和
  • 版本 4.1.17(9)-release (x86_64-unknown-cygwin))

for the former (3.1) for nn in (00..99) ; do ...works but for nn in (000..999) ; do ...does not work both will work on version 4.1 ; haven't tested printf behaviour (bash --versiongave the version info)

对于前者 (3.1)for nn in (00..99) ; do ...有效但for nn in (000..999) ; do ...不起作用,两者都适用于 4.1 版;尚未测试 printf 行为(bash --version提供版本信息)

Cheers, Jan

干杯,简