Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have tried different variations of the following but nothing seems to work. Basically when find gets executed nothing appears to happen. Below I present my bash function code and my output when I run it.

I am interested in understanding what happens with the code below and why it doesn't behave as when I type the command explicitly.

Also I got told I will not have access to rgrep in some boxes I will be working on and thus I try this approach for a generic solution to grepping for code etc.

function findin() {

if [ -z $1 ] ; then

    echo "Usage: findin <file pattern> <<grep arguments>>"
    return 1




echo -- "${fIn[1]}"
echo -- "'${fIn[2]}'"

find -type f -name "'${fIn[1]}'" -print0 | xargs -0 grep --color=auto ${fIn[2]}

And the output is:

$ ls
Server.tcl  Server.tcl~  test.cfg  vimLearning.txt
$ find -type f -name '*.txt' -print0 | xargs -0 grep --color=auto char
x      deletes char under cursor. NNx deletes NN chars to RHS of cursor.
r      type r and the next char you type will replace the char under the cursor.
$ findin '*.txt' char
-- *.txt
-- 'char'
share|improve this question
Why are you using fIn[1] and fIn[2]? What do you think that gives you that using properly named variables like fn=$1;shift;pattern="$@"? – Paul Tomblin Apr 5 '12 at 21:57
up vote 4 down vote accepted

The pattern you probably intended to use was *.txt, but you are telling find -name to use '*.txt', including the single quotes, which does not match any files. The expansion works as follows:

On the command line, when you type

$ find -name '*.txt'

your shell sees '*.txt' is quoted, so it strips the quotes and passes the contents, *.txt, to find.

In the function,

find -name "'$var'"

the shell expands $var to *.txt. Since the expansion occurred within double-quotes, the shell strips the double quotes and passes the contents, '*.txt', to find.

The solution is simple: remove the single quotes in find -name "'$var'".

I touched up your function for you:

findin () {
    if (( $# < 2 )); then
        >&2 echo "Usage: findin <file pattern> <grep arguments ...>"
        return 1    
    printf '%s\n' "-- ${pattern}"
    printf '%s ' "-- $@"
    find -type f -name "${pattern}" -print0 | 
            xargs -0 grep --color=auto "$@"
share|improve this answer
never mind previous comment if you saw it, I misread what you wrote. – Paul Tomblin Apr 5 '12 at 21:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.