Reputation:
I am trying to exclude a directory from a glob.
This works at the command line:
$ export exclude=BigDir
$ for d in ^$exclude/ ; do echo "$d" ; done
SmallDir/
SmallerDir/
$
But in a file it doesn't work at all
#!/bin/zsh
exclude=BigDir
for d in ^$exclude/ ; do echo "$d" ; done
Running ./test
or however I saved it prints the literal string
^BigDir/
How do I get it to correctly expand in the script file?
Upvotes: 1
Views: 1982
Reputation: 85530
You are incorrectly using the glob characters ?
used by the shell and the regular expression constructs ^
, $
. The for
loop in your example can not undergo a regex match to exclude the directory provided, since it undergoes only pathname expansion (aka. glob expansion)
Unless you let know the shell to treat ^
and $
as special by enabling extended glob options extglob
in bash
and extendedglob
in zsh
, you cannot achieve what you wanted to do.
So you probably just need
setopt extendedglob
print -rl ^BigDir*
meaning print anything except the the filenames matching with BigDir
.
Upvotes: 4