Search code examples
for-loopzshglob

zsh for loop exclusion


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?


Solution

  • 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