variableszshvariable-expansion

Baffling Zsh behavior on = (equal sign) precedes a Zsh $variable


When a = sign precedes a Zsh $variable expansion, I get the following behavior:

% m=$PATH;  echo =$m=
zsh: /usr/local/bin:/usr/sbin:/usr/bin= not found

Quite perplexing. What is happening, and why?


Solution

  • You've discovered = filename expansion.

    Per 14.7.3 ‘=’ expansion from the zsh docs:

    If a word begins with an unquoted ‘=’ and the EQUALS option is set, the remainder of the word is taken as the name of a command. If a command exists by that name, the word is replaced by the full pathname of the command.

    To disable this behavior, unset the EQUALS option:

    unsetopt EQUALS
    

    Here's an example of both behaviors:

    $ m=date
    
    $ setopt EQUALS
    $ echo =$m
    /usr/bin/date
    
    $ unsetopt EQUALS
    $ echo =$m
    =date