alxndr alxndr - 1 year ago 126
Bash Question

find -exec a shell function?

Is there a way to get

to execute a function I define in the shell? For example:

dosomething () {
echo "doing something with $1"
find . -exec dosomething {} \;

The result of that is:

find: dosomething: No such file or directory

Is there a way to get
to see

Answer Source

Since only the shell knows how to run shell functions, you have to run a shell to run a function. You also need to mark your function for export with export -f, otherwise the subshell won't inherit them:

export -f dosomething
find . -exec bash -c 'dosomething "$0"' {} \;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download