Subshell arguments in -exec parameter for find(1)

Solution 1:

exec does what it says: uses exec(). It doesn't involve a shell unless it's told to (-exec bash ...), and if it did you would still need single quotes to keep your interactive shell from interpreting the variable interpolations. (The shell is not magic and does not know that you intended those to be interpolated by something else.)


For example, when you use the following command:

find . -type f -exec echo $(file={}; echo ${file:0:5}) \;

your shell first performs process substitution by executing $(file={}; echo ${file:0:5}), which simply outputs {}, then executes the final command:

find . -type f -exec echo {} \;