zsh option question

What exactly does the 'globsubst' option in zsh do?
I noticed that array elements cannot be joined using strings containing square brackets, if I have the globsubst option on.

Here's an example. Let's say I have an array,

usernames=( Bob Sara Tim )

and I wanted an output of, Bob[return]Sara[return]Tim . I can get the output with the command,

echo ${(j:[return]:)usernames}

if the 'globsubst' option is off, but if it is on, I get a blank output.

Help is much appreciated.

G5 Dual 2.5 GHz, Mac OS X (10.4.4)

Posted on Jan 11, 2006 9:42 PM

Reply
7 replies

Jan 11, 2006 10:54 PM in response to Wataru Kagawa

Hi Wataru,
The GLOB_SUBST option, described in the zshoptions man page, causes zsh to perform filename generation (globbing) on the output of parameter expansion and command substitution. In your case, zsh attempts to glob the output of:

echo ${(j:[return]:)usernames}

This means that is searches the current working directory for files that match:

Bob[return]Sara[return]Tim

One of the cooler things about zsh is the fact that you can use regular expression style character classes in globbing. Thus, zsh attempts to find a file with "Bob" as the first three letters in the name, one of the letters in the word "return" as the fourth character, "Sara" as the fifth through the eighth characters, one of the letters in the word "return" as the ninth character and "Tim" as the tenth through the twelfth characters. I assume that your empty output indicates that you have no such file. Gee, I thought everybody had one of those!

You must also have the NULL_GLOB option set if you got no error message. I don't have that set and the output of the above echo was:

zsh: no matches found: Bob[return]Sara[return]Tim

I guess I don't have one of those files either but I've been meanin' t' get me one.
--
Gary
~~~~
In science it often happens that scientists say, 'You know that's a really good argument; my position is mistaken,' and then they actually change their minds and you never hear that old view from them again. They really do it. It doesn't happen as often as it should, because scientists are human and change is sometimes painful. But it happens every day. I cannot recall the last time something like that happened in politics or religion.
-- Carl Sagan, 1987 CSICOP keynote address

Jan 12, 2006 6:19 PM in response to Gary Kerbaugh

Hi Gary,

Thank you for the nice, clear explanations. I also got help yesterday from several people subscribed to the zsh mailing list. They showed me couple of ways to get the desired output with the glob_subst option turned on.

usernames=( Bob Sara Tim )
echo "${(j:[return]:)usernames}"
echo ${(j:[return]:)~~usernames}

I have another issue that I hope you (or Bill Scott or someone) can give me some explanations. The glob_subst option appears to affect some of the arguments inside my /etc/zshrc file, but not all. For example, the environment.local file gets sourced by the following argument regardless of the glob_subst option.

if [[ -f $ZDOT/environment.local ]]; then
source $ZDOT/environment.local
fi

However, the aliases.mine file fails to get sourced by the following argument if the glob_subst option is turned off.

if [[ -f $ZDOT_USER/aliases.mine ]]; then
source $ZDOT_USER/aliases.mine
fi

I have,
export ZDOT=/Library/init/zsh
export ZDOT_USER=~/Library/init/zsh
in my /etc/zshenv file.

Why does the sourcing of aliases.mine gets affected by glob_subst but not environment.local?

Jan 12, 2006 10:22 PM in response to Wataru Kagawa

Hi Wataru,
I'm not going to be as clear tonight as I was last night because I worked about 16 hours today and I've got to get to bed. I would guess that the issue is tilde, '~'.

Here's a quote from the zshoptions man page on GLOB_SUBST:
Treat any characters resulting from parameter expansion as being eligible for file expansion and filename generation, and any characters resulting from command substitution as being eligible for filename generation.
File expansion is the replacement of the tildes with home directories. Thus, without GLOB_SUBST, file expansion doesn't occur after parameter expansion. Of course, I don't understand why it doesn't occur in the assignment statement. I'm not sure if I would be able to figure that one out even if I had enough information. I'll look at my scripts and see if I can find anything similar. What other "glob options" do you have set?
--
Gary
~~~~
Laugh, and the world ignores you.
Crying doesn't help either.

Jan 13, 2006 2:48 PM in response to Wataru Kagawa

Hi Wataru,
In the case where you use double quotes,

export ZDOT_USER="$HOME/Library/init/zsh"

you are performing the parameter expansion at the time of the assignment instead of in the source statement. It should work the same way with the tilde if you just leave off the quotes.

Thanks for listing the glob options. The reason I asked is that I was trying to figure out why file expansion wasn't occurring in the assignment statement from your second post. After you posted the one with the quotes, all was clear. I might look through those glob options and try some but I don't like GLOB_SUBST. I would rather not set the option and turn it on when I need it with the tilde, like:

echo ${(j:[return]:)~usernames}

Too many levels of substitution can make it difficult to anticipate behavior or debug whereas if I explicitly turn it on, I'm going to expect it. (hopefully 🙂 )
--
Gary
~~~~
Fortune finishes the great quotations, #3

Birds of a feather flock to a newly washed car.

Jan 13, 2006 5:37 PM in response to Gary Kerbaugh

Hi Gary,

Oops. I left out the single quotes in my second post. I am sorry for the confusion. So if I were to use the tilde, and if the GLOB_SUBST option is set, expansion does not necessarily have to take place at the time of the assignment, because it can take place in the source statement. However, if the the GLOB_SUBST option is not set, expansion has to take place at the time of the assignment. (Right?)

Thanks also for suggesting that it might be a good idea to keep the GLOB_SUBST option turned off, and turning it on only when I need it.

Jan 13, 2006 6:54 PM in response to Wataru Kagawa

Hi Wataru,
Yes, you can eliminate the quotes in the assignment statement and file expansion will occur then. That's what I would do because it doesn't make sense to prevent the occurrence of an operation that eventually must take place. However, it doesn't really matter when it occurs and as you point out in the second paragraph, you can turn on GLOB_SUBST anytime you want. Thus, you can leave the quotes if you want and use a source statement like:

source ${~ZDOT_USER}/aliases.mine

Of course you would need to do the same in the conditional preceding the source statement and every other time you use the variable.
--
Gary
~~~~
Delusions are often functional. A mother's opinions about
her children's beauty, intelligence, goodness, et cetera ad
nauseam, keep her from drowning them at birth.
--Robert Heinlein

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

zsh option question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.