Bash – find command with parameters in variable

bashfind

> find /etc -name 'shells'
/etc/shells     # good !!

> SEARCH="-name 'shells'"; find /etc $SEARCH
# nothing found - bad !!

Why "find" command can't take params in variable?

Other commands work fine in such mode. It is probably related with spaces and parsing. How can I at first construct params in variable and then execute "find" with this params?

To be clear, I want to make the chain of -name xxxx -o -name yyyyy -o -name zzzzz and then find all the files by one run

Best Answer

your problem is the simple quotes are not interpreted as such, but as being in your parameters.

You think you've executed this:

find /etc -name 'shells'

When in fact you've executed this:

find /etc -name \'shells\'

Keep it in mind: In bash, simple quotes inside double quotes are not ignored.

So the solution is to not put any simple quotes:

SEARCH="-name shells"; find /etc $SEARCH

A better solution is to use quotes and then use eval:

SEARCH="-name 'shells'"; eval " find /etc $SEARCH"

Security problem: Never, Ever use user-supplied information in an eval argument.