如何从shell向bash函数传递参数

如何从shell向bash函数传递参数,bash,shell,parameter-passing,Bash,Shell,Parameter Passing,我在中定义了以下函数。bash\u aliases findphp () { find -name '*.php' -exec grep -H --color "$@" "{}" \; } 它允许我运行类似于findphp'function x'的程序,以查找php文件中对function x的所有引用 现在我想在bash中将文件类型作为参数传递给search,例如js、css、ctp 我定义了下面的函数和.bash_别名 myfind () { find -name '*.

我在
中定义了以下函数。bash\u aliases

findphp () {
    find -name '*.php' -exec grep -H  --color "$@" "{}" \;
}
它允许我运行类似于
findphp'function x'
的程序,以查找php文件中对
function x
的所有引用

现在我想在bash中将文件类型作为参数传递给search,例如js、css、ctp

我定义了下面的函数和.bash_别名

myfind () {
    find -name '*.$1' -exec grep -H  --color "$2" "{}" \;
}
但它不起作用,我试着用单引号,双引号括住位置参数 无济于事


如何定义将文件扩展名和搜索字符串作为命令行参数的函数?

单引号可防止变量扩展。使用
“*.$1”

尝试以下方法:

findphp () {
    find . -name "*.$1" -exec grep -H --color "${@:2}" "{}" \;
}

如果你也想要文件名

findphp () {
    find . -name "*.$1" -print0 | xargs -0 grep -H --color "${@:2}"
}