murpholinox
murpholinox

Reputation: 673

Why "seq -w" does not work in a for loop, compared with "seq"?

for i in `seq -w 01 10`; do echo "$i más 1 = $(( $i + 1 ))" ; done

01 más 1 = 2
02 más 1 = 3
03 más 1 = 4
04 más 1 = 5
05 más 1 = 6
06 más 1 = 7
07 más 1 = 8
bash: 08: value too great for base (error token is "08")

for i in `seq  01 10`; do echo "$i más 1 = $(( $i + 1 ))" ; done

1 más 1 = 2
2 más 1 = 3
3 más 1 = 4
4 más 1 = 5
5 más 1 = 6
6 más 1 = 7
7 más 1 = 8
8 más 1 = 9
9 más 1 = 10
10 más 1 = 11

Is this "a good way"? I was making a little script in bash where I need to input two files to a program. The first file is foo_02.txt and the second one is foo_01.txt

Upvotes: 1

Views: 699

Answers (1)

Barmar
Barmar

Reputation: 782105

In arithmetic expressions, numbers beginning with 0 are treated as octal, so they can't have digits 8 or 9. Instead of using seq -w, add the zero padding when you display the message, using printf.

for i in `seq 1 10`; do
    printf "%02d más 1 = %02d\n" $i $(( i + 1))
done

After %, the 0 modifier means to pad with zeroes, and 2 means the field width is 2 characters.

Upvotes: 3

Related Questions