error-handlingzsh

ZSH function not working with a "Missing end of string" error


I'm teaching myself to write zsh functions and I'm stumped right away with a string error I don't understand. I have this function:

function copyToDrafts() {
  print($1)
}

in my command line editor (Terminal) I type:

copyToDrafts "test"

and receive this error:

copyToDrafts:1: missing end of string

I couldn't find any explanation on the error message and can't see anything wrong with what I am passing, though obviously something is wrong. Any help would be appreciated.


Solution

  • The parentheses are not part of the syntax; they are interpreted as introducing a glob qualifier on the pattern print. After parameter expansion, the pattern to be evaluated is

    print(test)
    

    with the following glob qualifiers:

    1. t - match files named print that have their sticky bits set
    2. e execute a shell command. s acts as the delimiter, but there is no "closing" s, which produces the observed error.

    You simply need to drop the parentheses.

    copyToDrafts () {
      print $1
    }