Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: variable expanding to `*` matches a literal `*` in case cond. construct



On Sun, Dec 15, 2019 at 11:41:38AM +0300, Oğuz wrote:
> Although one would expect it to print "match"
> 
>     zsh -c 'case foo in $1) echo match; esac' sh '*'
> 
> above command doesn't print anything; if `*` is a result of a variable
> expansion,
> it matches only a literal asterisk. All the other shells I could find
> (including ksh
> from 2010) does print "match". I know this is a compliancy issue and zsh
> doesn't
> claim to be compatible with any other shell, I really wonder; was this
> intended?
> If yes, what was the incentive for that, what's it good for?
> 
> -- 
> Oğuz

In zsh, I suppose you would use ${~1}

    $ zsh -c 'case foo in ${~1}) echo match; esac' sh '*'
    match
    $ zsh -c 'case foo in ${~1}) echo match; esac' sh '??'
    $ zsh -c 'case foo in ${~1}) echo match; esac' sh '???'
    match

From the manual:

${~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.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden



Messages sorted by: Reverse Date, Date, Thread, Author