Search code examples
linuxbashshellzshrc

what is the matching strategy of commands in shell like bash and zsh?


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)?


Solution

  • 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.