[plug] Lists in BASH

stephen humble steve at newton.dialix.com.au
Sat Aug 19 20:47:41 WST 2000


try something like this (default method is space delimited fields)

data="matt leon tony"

then you can print each out using

for name in $data
echo $name 
done

can add john to the list  using

data=`echo $data john`




Garth Atkinson wrote:
> 
> Bash 2.0 has associative arrays, bash 1.4 doesnt. I'm not sure what
> you are trying to do with example you supplied.
> 
> Do you want variable lists like in Python?
> 
> ie
> 
> data="matt,leon,tony"
> 
> for reprobate in data
>         print reprobate
> 
> would give you:
> 
> matt
> leon
> tony
> 
> To achieve the above in bash you need something like this
> (untested):
> 
> data="matt,leon,tony"
> 
> IFS=","
> data="NOTZEROLENGTH"
> 
> while [ -z "$data" ] ; do
>         echo $data | read reprobate data
>         echo $reprobate
> done
> 
> (I am assuming data gets set to zero length in the fourth loop)
> 
> Garth
> 
> skribe wrote:
> >
> > How do I assign a text list to a variable in bash?
> >
> > ie
> >
> > data=matt,leon,tony




> >
> > skribe
> >         Purity:                           62.0%
> >         Corruption:                    38.0%
> >         Insanity:                        36.3636363636364%
> >         Weirdness factor:           31%
> >         Experience Level:           JonKatz Wannabe
> >         Medieval Career:            Black Knight



More information about the plug mailing list