bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash 3.2.25 not expanding subscript...


From: Bernd Eggink
Subject: Re: Bash 3.2.25 not expanding subscript...
Date: Tue, 12 Feb 2008 10:13:25 +0100
User-agent: Thunderbird 2.0.0.9 (X11/20071031)

Brad Diggs schrieb:

In short the bug is the result of failure to expand the
subscript of an array if the subscript is a variable.
The following script should return a list of files with a preceding (File <#>: ). However, it does not work that way because the integer variable (${d}) used in the subscript of the array statement (FileList[${d}]=${File}) does not get properly expanded.

#!/bin/bash
declare -a FileList=('')
declare -i d=0

ls -1| while read File
do
   FileList[${d}]=${File}
   d=$((10#${d}+1))
done

This is normal bash behaviour, see FAQ E4. As bash executes _all_ parts of a pipe in subshells (in contrast to ksh, where the last component is executed in the current shell), the variable 'FileList' being assigned here is local to the subshell. After the loop the variable 'FileList' declared in line 1 (which happens to have the same name, but that doesn't matter) is unchanged.

Try this instead:

        while read File
        do
           FileList[d]=$File
           (( d=d+1 ))
        done <<<"$(ls -1)"

Greetings,
Bernd

--
Bernd Eggink
monoped@sudrala.de
http://sudrala.de




reply via email to

[Prev in Thread] Current Thread [Next in Thread]