0

I am now using zsh shell, but I assume that its matching strategy of commands is the same as bash. Here is the thing:

I want to make an alias command, which create a new .cpp file using a template file and take the first argument as the new file name. I also write an alias for Java template.

Here are the functions in .zshrc:

function cppgen()
{
  cp ~/Documents/Templates/cpp_template.cpp ./$1.cpp
  vim ./$1.cpp
}

function javagen()
{
  cp ~/Documents/Templates/java_template.java ./$1.java
  vim ./$1.java
}

Weiredly, only the javagen alias works. When I use cppgen, the shell prints:

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

After struggling for a while, I finally realized that the shell treats my cppgen command as cp. After I changed the alias name from cppgen to cgen, everything works fine.

So does it mean that shell doesn't try to match the exact command but the shortest matching command(I guess)?

pynexj
  • 19,215
  • 5
  • 38
  • 56
luochenhuan
  • 1,057
  • 11
  • 17
  • I haven't used zsh much, but I'd be very surprised if a function named `cppgen` were to override the `cp` command. How exactly did you invoke your `cppgen` function (with what argument(s))? – Keith Thompson Mar 08 '15 at 06:35
  • Show the results of calling `cppgen` -- with a realistic example of your usage mode -- after running `set -x`. – Charles Duffy Mar 08 '15 at 06:39
  • Aliases take priority over functions; take a look at your list of aliases and see if you already have cppgen defined as an alias. – racraman Mar 08 '15 at 06:41

1 Answers1

0

Thanks all for the comments. Now I could set alias as cppgen. I didn't change anything. Not sure what happened... Maybe I need more coffee before posting in StackOverflow.

luochenhuan
  • 1,057
  • 11
  • 17