Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

As an example, here is the code:

#!/bin/bash
s=00
until [ $s -eq 60 ] 
do
    let "s += 1"
    echo $s
done

$s will be 1, 2, 3 etc, but not 01, 02, 03 etc. How can I avoid this deletion of 0? So, I need $s to be 01-09, then 10, 11, etc.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Add the zeros when you print it.

#!/usr/bin/env bash
s=0
until ((++s == 60 )); do
    printf '%02d\n' "$s"
done

Numbers with leading zeros will be treated as octal, so values like 08 and 09 will fail. Therefore it's best to operate on the numbers without the leading zeros and only add the leading zeros when you need to print it.

Also see http://mywiki.wooledge.org/BashFAQ/018

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.