user3310334
user3310334

Reputation:

glob pattern doesn't expand inside file in zsh

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

Answers (1)

Inian
Inian

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

Related Questions