[nzlug] bash help - for loop listing dirs

Rex Johnston spammeatyourperil at sclnz.com
Thu Feb 22 08:15:11 NZDT 2007


Howard wrote:

> I have a for loop which I thought would need to be thus:
> 
> for i in `ls -1`
> do
> cd $i
> blah...

you'll need to either spawn a subshell for blah, or reference your
base directory when cd`ing.

i.e.

for i in `ls -1`
do
     (cd "$i" && blah)
done

or

base=`pwd`
for i in `ls -1`
do
     cd "$base"/"$i"
     blah
done

Cheers, Rex

. disclaimer.sh




More information about the NZLUG mailing list