Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZSH for loop array variable issue

I'm working in zsh, but I'm sure that bash instructions will also be helpful.

I need to have a for loop that goes through the values stored in the array lw and then launches a shell script, based on the name value stored in the array.

So far, this is what I've come up with:

$lw=('plugin1' 'plugin2' 'plugin3')  for i in $lw;   do . ~/Library/Rogall/plugins/$lw[$i]/lw.prg end; done 

Running this gives me an error saying that it can't find ~/Library/Rogall/plugins//lw.prg. It appears as if it's ignoring my variable all together.

Any ideas where I've messed up?

like image 761
user1296965 Avatar asked Jun 04 '12 20:06

user1296965


1 Answers

It's actually much simpler than that:

lw=('plugin1' 'plugin2' 'plugin3')  for i in $lw; do   . ~/Library/Rogall/plugins/$i/lw.prg end done 

In summary:

  • Assign to foo, not $foo (the shell would try to expand $foo and assign to whatever it expands to; typically not useful)
  • Use the loop variable directly; it contains the array value rather than the index
like image 93
Jan Krüger Avatar answered Oct 05 '22 03:10

Jan Krüger