MeaCulpa
MeaCulpa

Reputation: 911

zsh: disable glob in variable substitution

In ksh I can pass variable to a string operator pattern:

m=h*; a=shy;  print ${a%%${m}}

Will give me the result 's', but in zsh, the * seems to be extanded and no way to avoid this:

m=h*; a=shy; print ${a%%${m}}
m=h*; a=shy; noglob print ${a%%${m}}

Will both still give me 'shy'. So how can I have the * pattern passed to string operator?

Upvotes: 4

Views: 1426

Answers (1)

MeaCulpa
MeaCulpa

Reputation: 911

I have the solution, apply a '~' in the pattern.

m=h*; a=shy;  print ${a%%$~m}

Quoting from man zshexpn:

${~spec}
Turn on the GLOB_SUBST option for the evaluation of spec; if the ~ is doubled, turn it off. When this option is set, the string resulting from the expansion will be interpreted as a pattern anywhere that is possible, such as in filename expansion and filename generation and pattern-matching contexts like the right hand side of the = and != operators in conditions.

Upvotes: 4

Related Questions