3

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?

1 Answers1

2

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 . "$@"
  • 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