linux - printf 从左侧填充除零以外的数字

标签 linux bash shell

我有 a=10 var,我想用整数 1 从左侧填充,填充 5。

输出应如下:

11110

当我在做的时候:

printf "%05d\n" "${a}" 

它给了我00010

最佳答案

根据 bash printf手册页中,它仅支持零填充,因此您不能直接使用零以外的整数填充。

您可以通过字符串打印或通过操作来完成

a=10
printf '%s\n' 111${a}

 printf "% 5d\n" "10" | tr ' ' '1'

关于linux - printf 从左侧填充除零以外的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664477/

相关文章:

c - 多线程编程使用全局变量时出现的奇怪问题

linux - 无法在@INC 中找到 Spreadsheet/ParseExcel.pm,尽管它似乎已安装

linux - Crontab 中的 Perl 脚本没有在一夜之间移动文件

bash - gitlab runner 最后抛出 "Cleaning up file based variables 00:01 ERROR: Job failed: exit code 1"

linux - 示例 Bash 脚本或命令以检索目录中的数字文件

bash - 重命名子目录中的所有文件,同时保持相同的位置

bash - 使用 awk 比较 2 个不同文件的第一列

shell - MacOS Sierra 上的 sudo 命令不遵守 timestamp_timeout

linux - 为什么我必须在容器中使用 bash -l -c?

linux - 如何在 bash 中隐藏 "Created new window in existing browser session"