How to pass multiple file types search pattern as argument to find?

How can I pass $var_find variable as argment to find command?

test.sh

 
var_find=' \( -name "*.xml" -o -name "*.jsp" \) '
 
echo "${var_find}"
 
find . -type f ${var_find} -print
 
# Below statement works fine.. I want to replace this with the above..
#find . \( -name "*.xml" -o -name "*.jsp" \)  -print

output -->

I tried escape character to escape " quotes,, and different types of braces for calling var_find,, none seem to be working..

Could someone help with this ?

Try:

eval find . -type f "${var_find}" -print
1 Like

Thanks Mr. Scrutinizer,, that makes it work..

The reason, why this fails (and why Scrutinizers solution works) is subtle: by declaring the variable "var_find" with some content you make this content one string, even if this string contains several blanks, which usually separate strings in the shell.

Lines are read in fields separated by so-called "field separators" (white space, semicolon, etc.) by the shell. But in quoted strings these field separators are ignored. The following script will show that:

#!/bin/ksh

print - "Number of arguments: $#"

exit 0

call the script with ./script a b c and it will print the expected value 3. Issue ./script a "b c" and it will print only 2, because "b c" is considered to be one string.

To come back to your find-command: find knows how to handle the "-name"-option and the "-name"-option knows that it expects exactly one argument: a filename mask. But find doesn't know about a "\( -name ".xml" -o -name ".jsp" \)"-option and this is what it complains about.

I hope this helps.

bakunin

It still feels like I do not fully understand what really goes behind the scenes in shell..
But I will try to remember this,, for now..