Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running the following bash command:

find . \( -iname '*.aif' -o -iname '*.pdf' -o -iname '*.exe' -o -iname '*.mov' \
    -o -iname '*.doc' \) -exec rm -f {} \;

I'm running the same parameters in another call to find later on in the script. Is there any way to store the \( -iname '*.aif' -o -iname '*.pdf' -o -iname '*.exe' \ -o -iname '*.mov' -o -iname '*.doc' \) part in a variable?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

In ksh, bash or zsh, use an array variable.

find_parameters=(\( -iname '*.aif' -o … -iname '*.doc' \))
find . "${find_parameters[@]}"

In other shells, there's no good way (there are complex and brittle ways but I don't recommend them unless you really need them). If you need a single array, you can use the positional parameters:

set \( -iname '*.aif' -o … -iname '*.doc' \)
find . "$@"
share|improve this answer
    
That works fine, thanks. So is that an array containing \(, -iname, '*.aif', -o, -iname, ...? I've just never seen an array definition like that. –  Matt Alexander Jan 27 '11 at 20:35

Your Answer

 
discard

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.