Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash printing and incrementing array values

I'm making a bash script in which I need to print a number while it's incremented like this:

0000
0001
0002
0003
0004

I have made this but is not working:

#!/bin/bash
i=0
pass[0]=0
pass[1]=0
pass[2]=0
pass[3]=0
for i in $(seq 1 9)
    pass[3]="$i"
    echo ${pass[*]}
done

I paste the script on cli and i get this.

$ ~ #!/bin/bash
$ ~ i=0
$ ~ pass[0]=0
$ ~ pass[1]=0
$ ~ pass[2]=0
$ ~ pass[3]=0
$ ~ for i in $(seq 1 9)
>     pass[3]="$i"
bash: error sintáctico cerca del elemento inesperado `pass[3]="$i"'
$ ~     echo ${pass[*]}
0 0 0 0
$ ~ done
bash: error sintáctico cerca del elemento inesperado `done'
$ ~ 
like image 203
fpilee Avatar asked Apr 11 '26 04:04

fpilee


1 Answers

Use this pure bash script:

for ((i=0; i<10; i++)); do
   printf "%04d\n" $i
one

OUTPUT:

0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
like image 166
anubhava Avatar answered Apr 13 '26 04:04

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!