LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Capturing info in an array is not working (http://www.linuxquestions.org/questions/linux-general-1/capturing-info-in-an-array-is-not-working-4175411558/)

dth4h 06-15-2012 02:27 AM

Capturing info in an array is not working
 
I found this code
Code:

#! /bin/bash

i=1
ls | while read line
do
    array[ $i ]="$line"
    (( i++ ))
done

from this post


However the code is not working for me. Anytime I try to echo one of the array entries it doesn't display anything.
Is this code to old to work? It doesn't seem that old.

I run the code in the terminal like this:
Code:

i=1; ls | while read line;do array[$i]="$line"; (( i++ )); done
And it doesn't work.

This line works:
Code:

ls | while read line;do echo "$line"; done
It lists all the stuff in the current directory I am in on separate lines.

I am thinking maybe just the array part of the code is wrong.

dth4h 06-15-2012 04:15 AM

Nevermind. I found the answer.

Code:

i=1
while read line
do
    array[$i]="$line"
    (( i++ ))
done < <(ls)



All times are GMT -5. The time now is 11:18 PM.