Array length: ls and sort

Hi there,

I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird.



    files_info="$(find $input_dir -name "*_CHR$i.info"  | sort )"

    printf ${#files_info[@]}"\n" #print length 

#--loop through array - it works fine
    for x in ${files_info[@]}
    do
        printf "$x\n"
    done

When I loop through the array it works fine. But when I want to print length of array it print 1.
When I debug it with -x, I see '\n'
May be new line character is messing length?

Please guide.

You're not assigning an array but a scalar variable, thus the result 1 is correct. Unfortunately, you don't mention your environment, so I can't tell you how to define an array.

Linux #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64 GNU/Linux

bash:

declare -a myarray=( ` ls command goes here ` )

Never knew about declare. Sorry. When to use it?

For at least bash and ksh arrays, you could replace your code with:

    files_info=( $(find $input_dir -name "*_CHR$i.info"  | sort ) )

    printf ${#files_info[@]}"\n" #print length 

#--loop through array - it works fine
    printf "%s\n" "${file_info[@]}"

as long as none of the pathnames returned by find contain any <space>s or <tab>s.

Thanks don.
($(
this should be good!