[Top][All Lists]

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

Re: Bash 3.2.25 not expanding subscript...

From: Brad Diggs
Subject: Re: Bash 3.2.25 not expanding subscript...
Date: Tue, 12 Feb 2008 09:46:43 -0600


Thank you so very much!!!  I would have never figured that out
on my own.  I went back to the Advanced Bash Scripting Guide 
(by Mendel Cooper) to see if this example would make a good 
addition.  Searching on your use of Here Strings (e.g. <<<). 
I found on page 326 (18.1. Here Strings) a the following method 
that also works quite well.

read -r -a FileList <<< $(ls -1)

I wish that I had noticed the Here Strings feature before.  That
is a great feature.

Thank you both for your time and contributions!


Advanced Bash Scripting Guide

On Tue, 2008-02-12 at 10:13 +0100, Bernd Eggink wrote:
> 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

The Zone Manager

reply via email to

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