How to format a number in bash, possibly with printf?

前端 未结 4 1807
遇见更好的自我
遇见更好的自我 2021-01-15 07:12

This so simple script is failing :

#!/bin/bash
n=1
printf -v fn \"%05d\" $n
echo $fn

with

3: printf: Illegal option -v
         


        
4条回答
  •  难免孤独
    2021-01-15 07:39

    As other answers tell you that older BASH version versions don't support printf -v but here is what you can do that in older BASH to set fn:

    #!/bin/bash
    n=1
    fn=$(printf "%05d" "$n")
    echo "$fn"
    

提交回复
热议问题