After much searching on IRC someone pointed me to the following answer
find . -iname "*.xml" -exec bash -c 'echo "$1"' bash {} \;
or for my example (with the string cut removed to save confusion)
find . -iname "*.xml" -exec bash -c 'gmake NAME="$1"' bash {} \;
The way this works is bash takes the parameters after -c
as arguments, bash {}
is needed so that the contents of {}
is assigned to $1
not $0
, and bash
is used to fill in $0
. It's not only a placeholder as the contents of that $0
is used in error messages for instance so you don't want to use things like _
or ''
To process more that one file per invocation of bash
, you can do:
find . -iname "*.xml" -exec bash -c '
ret=0
for file do
gmake NAME="$file" || ret=$?
done
exit "$ret"' bash {} +
That one has the added benefit that if any of the gmake
invocations fails, it will reported in find
's exit status.
More info can be taken from http://mywiki.wooledge.org/UsingFind#Complex_actions
find . -exec TMP=1; echo $TMP \;
But this morning this gives the error "find: missing argument to `-exec' ;" and I'm baffled what the heck I did that worked and why this is giving such an unexpected error! I know I have done this lots of times even with a pipe and now everything I try is an error. Evenfind . -exec TMP=1 \;
gives errors!