To provide additions and clarifications to some of the other answers, if you are using the bulk option for exec
or execdir
(-exec command {} +
), and want to retrieve all the positional arguments, you need to consider the handling of $0
with bash -c
.
More concretely, consider the command below, which uses bash -c
as suggested above, and simply echoes out file paths ending with '.wav' from each directory it finds:
find "$1" -name '*.wav' -execdir bash -c 'echo "$@"' _ {} +
The Bash manual says:
If the -c
option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to positional parameters, starting with $0
.
Here, 'echo "$@"'
is the command string, and _ {}
are the arguments after the command string. Note that $@
is a special positional parameter in Bash that expands to all the positional parameters starting from 1. Also note that with the -c
option, the first argument is assigned to positional parameter $0
.
This means that if you try to access all of the positional parameters with $@
, you will only get parameters starting from $1
and up. That is the reason why Dominik's answer has the _
, which is a dummy argument to fill parameter $0
, so all of the arguments we want are available later if we use $@
parameter expansion for instance, or the for
loop as in that answer.
Of course, similar to the accepted answer, bash -c 'shell_function "$0" "$@"'
would also work by explicitly passing $0
, but again, you would have to keep in mind that $@
won't work as expected.
$0
. – Forewent