Padding zeros in a string

前端 未结 7 616
情话喂你
情话喂你 2020-12-24 10:03

I\'m writing a bash script to get some podcasts. The problem is that some of the podcast numbers are one digits while others are two/three digits, therefore I need to pad t

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 11:00

    Use backticks to assign the result of the printf command (``):

    n=1
    wget http://aolradio.podcast.aol.com/sn/SN-`printf %03d $n`.mp3
    

    EDIT: Note that i removed one line which was not really necessary. If you want to assign the output of 'printf %...' to n, you could use

    n=`printf %03d $n`
    

    and after that, use the $n variable substitution you used before.

提交回复
热议问题