Answer the question
In order to leave comments, you need to log in
How to create an array with leading zeros in Bash?
It would seem that what is easier:
#!/bin/bash
hr=(00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
for i in ${hr[@]}; do
echo ${hr[i]}
done
[email protected]:$ ./stat.sh
00
01
02
03
04
05
06
07
./stat.sh: строка 7: 08: слишком большое значение для основания (неверный маркер «08»)
Answer the question
In order to leave comments, you need to log in
for i in ${hr[@]}; do echo ${hr[i]} done
Bash thinks that if a number starts with 0, then it is in octal and the number 8 is not in it. What to do?
echo ${hr[i]}
#!/bin/bash
hr=(00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
for i in ${hr[@]}; do
echo $i
done
There is no such thing as "a number with a leading zero".
The leading zero is not part of the number, but simply output formatting.
So just use printf with the format, in your case %02d (d is a number, 02 is 2 digits with a leading zero)
#!/bin/bash
hr=(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
for i in ${hr[@]}; do
printf "%02d\n" ${hr[i]}
done
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question