zshmanualssystem-variable

Unable to get a system variable work for manuals


I have the following system variable in .zshrc

manuals='/usr/share/man/man<1-9>'

I run unsuccessfully

zgrep -c compinit $manuals/zsh*

I get

zsh: no matches found: /usr/share/man/man<1-9>/zsh*

The command should be the same as the following command which works

zgrep -c compinit /usr/share/man/man<1-9>/zsh*

How can you run the above command with a system variable in Zsh?


Solution

  • From my investigations, it looks like zsh performs <> substitution before $ substitution. That means when you use the $ variant, it first tries <> substitution (nothing there) then $ substitution (which works), and you're left with the string containing the <> characters.

    When you don't use $manuals, it first tries <> substitution and it works. It's a matter of order. The final version below shows how to defer expansion so they happen at the same time:

    These can be seen here:

    > manuals='/usr/share/man/man<1-9>'
    
    > echo $manuals
      /usr/share/man/man<1-9>
    
    > echo /usr/share/man/man<1-9>
      /usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
      /usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
      /usr/share/man/man7 /usr/share/man/man8
    
    > echo $~manuals
      /usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
      /usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
      /usr/share/man/man7 /usr/share/man/man8