加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Bash中增加零填充int

发布时间:2020-12-16 01:08:38 所属栏目:安全 来源:网络整理
导读:我有一组循环记录.这个号码的范围从0000001到0089543,可以调用UIDX. 如果我尝试类似的东西: for ((i=0; i 0089543; i++)); do ((UIDX++))done 计数器增量1,2,3,4,而不是我需要的0000001,0000002 …… 什么是填补领先零的最佳方式? 使用printf命令格式化带
我有一组循环记录.这个号码的范围从0000001到0089543,可以调用UIDX.

如果我尝试类似的东西:

for ((i=0; i< 0089543; i++)); do
    ((UIDX++))
done

计数器增量1,2,3,4,而不是我需要的0000001,0000002 ……

什么是填补领先零的最佳方式?

使用printf命令格式化带有前导零的数字,例如:
for ((i = 0; i < 99; ++i)); do printf -v num '%07d' $i; echo $num; done

来自man bash:

printf [-v var] format [arguments] Write the formatted arguments to the standard output under the control of the format. The -v option causes the output to be assigned to the variable var rather than being printed to the standard output.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读