1

In my .bash_profile, I have a function that returns all php files containing the parameter string passed in:

summon() {
 "find . -name '*.php' -exec grep -ril '$1' '{}' \;"
}

When I am on my command line (mac) and I run summon foo, I get the error:

-bash: find . -name '*.php' -exec grep -ril 'foo' '{}' \;: command not found

But if I just copy/paste the find . -name '*.php' -exec grep -ril 'foo' '{}' \; into the command line, then it works properly, returning all of the php files that contain the string 'foo'.

Does anyone have any idea why the function is not being evaluated?

3 Answers 3

3

Just remove the quotes from your summon function. By quoting it, you are telling it to look for a command called find . -name '*.php' -exec grep -ril '$1' '{}' \; rather than a command called find with arguments of . -name '*.php' -exec grep -ril '$1' '{}' \; There is a good reason for this; consider if there were an application whose name contained a space (let's call it foo bar). If not for this quoting syntax, the program would be more difficult to execute from bash, because typing foo bar would try to run the command foo with argument bar, as opposed to running foo bar (As a side note, if this were the case, you could also run it by escaping the space: foo\ bar). Of course, it is considered bad form to name an executable something containing a space for this reason of adding complexity to run the command.

Your function should look like this:

summon() {
  find . -name '*.php' -exec grep -ril "$1" '{}' \;
}

Also see @gniourf_gniourf 's comment on this answer with a few more suggestions, including using -type f on the find command to limit the search to files and removing the unnecessary -r flag from grep, because all files passed there will be files.

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

5 Comments

If I do that, then no files are returned (even if I know there are files with 'foo' in them because I get files returned when I copy/paste the string into the command line without calling summon())
Put the $1 in double quotes instead of single. Single quotes causes the variable to not be expanded, instead passing the literal string "$1"
My pleasure! If an answer here solves your problem, please mark it as accepted if you don't mind :)
Yes I will, it is making me wait 3 more minutes for some reason. But could you change your suggested solution to have the double quotes around $1?
You could give these hints to OP: 1. use -type f. 2. grep's -r switch is useless. 3. use -exec grep ... {} +.
3

Loose the double-quotes around the find within the function.

summon() {
  find . -name '*.php' -exec grep -il "$1" '{}' +
}

Within double-quotes, shell tries to expand it, so that it can evaluate it as an expression, Shell-Expansion

2 Comments

But you need double quotes around $1: I have a function that returns all php files containing the parameter string passed. And a hint to OP to use -type f could be nice. (and grep's -r switch might be useless too).
And to upset Ed Morton you could also provide a GNU grep only answer :D.
0

Argument inside single quote is the problem. Try like the below

summon() {
  find . -name '*.php' -exec grep -ril "$1" {} \;
}

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.