tags:

views:

162

answers:

2

This is somewhat of a simple question, but for the life of me, I cannot figure out how to exclude something from a zsh for loop. For instance, let's say we have this:

for $package in /home/user/settings/*
do
  # do stuff
done

Let's say that in /home/user/settings/, there is a particular directory ("os") that I want to ignore. Logically, I tried the following variations:

for $package in /home/user/settings/^os (works w/ "ls", but not with a foor loop)
for $package in /home/user/settings/*^os
for $package in /home/user/settings/^os*

...but none of those seem to work. Could someone steer my syntax in the right direction?

+1  A: 

That for loop works for me if I set -o EXTENDED_GLOB (or setopt EXTENDED_GLOB).

Dennis Williamson
+2  A: 

It looks to me that the extra $ may be what's causing your grief.

Try this:

for package in /home/user/settings/^os; do
    echo "Doing stuff with ${package}..."
done

If you want to limit ${package} to just directories, use /home/user/settings/^os(/).

Also ensure that you have extendedglob set (which I think you do since ls works for you):

> set -o  | grep -i extendedglob
extendedglob          on
Johnsyweb
Gosh, silly error - thank you for your help! :)
ABach
+1 - I can't believe I missed that dollar sign.
Dennis Williamson
Often the simplest errors are the very hardest to spot. Happy to help.
Johnsyweb