Search code examples
zshzshrczsh-alias

Handling "?" character passed to ZSH function


I'm having problem with setting up simple function in ZSH.

I want to make function which downloads only mp3 file from youtube.

I used youtube-dl and i want to make simple function to make that easy for me

ytmp3(){
  youtube-dl -x --audio-format mp3 "$@"}

So when i try

ytmp3 https://www.youtube.com/watch?v=_DiEbmg3lU8

i get

zsh: no matches found: https://www.youtube.com/watch?v=_DiEbmg3lU8

but if i try

ytmp3 "https://www.youtube.com/watch?v=_DiEbmg3lU8"

it works. I figured out that program runs (but wont download anything) if i remove all charachers after ? including it. So i guess that this is some sort of special character for zsh.


Solution

  • The question mark is part of ZSH's pattern matching, similarly to *. It means "Any character".

    For instance, ls c?nfig will list both "config" and "cinfig", provided they exist.

    So, yes, your problem is simply that zsh is trying to interpret the ? in the URL as a pattern to match to files, failing to find any, and crapping out. Escape the ? with a \ or put quotes around it, like you did, to fix it.