Search code examples
zshzshrc

zsh: "reload:4 = not found" for function named "reload"


I'm trying to make a shortcut for myself to reload a site on a server, so I'm getting the site name via regex, and then trying to have zsh run a command to reload with the correct parameters.

I've tried numerous variations on this but I keep getting the error "= not found" which I don't understand.

function reload(){
    pwd=$(pwd) 
    [[ $pwd =~ .*\/custom\/.+\/(.+)(\/stage)? ]] && echo "$match[1]"
    read -q "REPLY?rc $match[1].stage reload"
    if[REPLY == ""] then
    cmd="rc $match[1].stage reload"
    $=cmd
}

Everything works until the last 1-2 lines.

Output expected: rc somesite.stage reload // then enter runs this command in zsh

Actual output:

$ currentDir % reload  
currentDirSiteName
rc currentDirSiteName.stage reload
reload:4: = not found


Solution

  • To explain the error message:

    You have the line

    if[REPLY == ""] then
    

    According to the parsing rules of zsh, a line such as A B C is broken on the spaces (with respect to quoting, of course), and the first word (i.e. A) is taken as a command to execute, while the other words are the parameters. In your case, breaking on the spaces yields the 4 words

    if[REPLY
    ==
    ""]
    then
    

    First, we see that you most likely don't have a command named if[REPLY. This by itself would already be an error, but zsh bails out earlier: Before it can even try to run the command, it has to prepare the parameters:

    The first parameter is ==, and parameters starting with an = sign undergo special expansion in zsh: For a word =foo, the shell tries to locate an executable named foo and replaces the word by the absolute path to this executable. You can try this out by typing echo =cat, which will output something like /usr/bin/cat. In your case, what follows after the = sign, is another = sign. zsh therefore tries to find a program named = in your path, and is unsuccessful with it. This is the cause of the error message.

    If you had written instead

    if [[ $REPLY == "" ]]; then
    

    the shell would have recognized that you have an if (which is a syntactic construct). After the if, it expects a command. [[ ... ]] is an internal command for evaluating certain types of expressions, and inside this construct, different parsing rules apply. For this reason, == does not undergo the expansion process described before, but is understood as an operator for testing equality.

    BTW, A more idiomatic way would be to write

    if [[ -z $REPLY ]]; then