2

I have the following function defined in .bash_aliases

findphp () {
    find -name '*.php' -exec grep -H  --color "$@" "{}" \;
}

It allows me to run something like findphp 'function x' to find all references to function x in php files

Now I want to pass the file type to search, example js, css, ctp as a parameter in bash

I defined the follwing function and sourced .bash_aliases

myfind () {
    find -name '*.$1' -exec grep -H  --color "$2" "{}" \;
}

But it's not working, I tried single quotes, double quotes around the positional parameters with no avail.

How could I define a function that takes the file extension and search string as arguments from the command line?

2 Answers 2

2

Single quotes prevent variable expansion. Use "*.$1"

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

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

or

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

if you want the filenames too

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.