Curly brace functions will run within the calling shell process, unless they need their own subshell which is:
- when you run them in the background with
&
- when you run them as a link in a pipeline
Redirections or extra env. variables won't force a new subshell:
hw(){
echo hello world from $BASHPID
echo var=$var
}
var=42 hw >&2
echo $BASHPID #unexports var=42 and restores stdout here
If you define the function with parentheses instead of curlies:
hw()(
echo hello world from $BASHPID
)
hw
echo $BASHPID
it will always run in a new process.
Command substitution $() also always creates processes in bash (but not in ksh if you run builtins inside it).
***if you want a proof, I can't give you. I've derived it from webpages, comments... I've read that statement somewhere sometime ago.
– Daniel Bandeira Jul 23 '20 at 15:06