Bash – Scope of variables when calling function from find

bashfindfunctionvariable

In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an example:

variable="Filename:"

myfunction() {
    echo $variable $1
}

export -f myfunction

find . -type f -exec bash -c 'myfunction "{}"' \;

This will output filenames but without the string "Filename:".

Is there perhaps a better way to invoke a function from find such that it is called for every file that find finds, and variables are still defined?

Best Answer

You could declare variable as an environment variable, i.e.,

export variable="Filename:"