1

I read file, but data of array exist only in loop. At the end of code i try to echo tab[3], but it is empty.

ls -t > lista.txt
N=$(wc -l lista.txt)
N=${N:0:1}
tab[$N]=0


let I=0
while read line 
do
    tab[$I]="$line"
    echo ${tab[$I]}
    ((I+1))
done <lista.txt
echo ${tab[3]} #no Value

1 Answer 1

1

((I+1)) doesn't change the value of $I. You have to assign the value to it

(( I = I + 1 ))

or use an increment

# You can use any of the following lines:
(( I++ ))
(( ++I ))
(( I += 1 ))
Sign up to request clarification or add additional context in comments.

1 Comment

@Xalion: Yes, because $I stays 0 and it changes and shows ${tab[0]} all the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.