On Sun, May 16, 2021 at 1:38 AM leodream <leodream2...@gmail.com> wrote: > > I created 2 scripts like below > <http://gnu-bash.2382.n7.nabble.com/file/t3303/Screenshot_20210516_163002.png> > > Running the test1.sh with bash 5.1 will have different PPID printed by the > last test2.sh call when it is executed in sub-shell. > For bash 4.3, the script will output consistent PPID. > <http://gnu-bash.2382.n7.nabble.com/file/t3303/Screenshot_20210516_163523.png> > > > It looks like something had been changed regarding the sub-shell execution > in later Bash versions?
Yes. See the following entries in the 5.1 changelog (https://git.savannah.gnu.org/cgit/bash.git/tree/CHANGES-5.1?h=88bdb448b40b81a8dc1b8c2682bb5bb968d8213b#n134): $ grep optimize CHANGES-5.1 c. Bash attempts to optimize away forks in the last command in a function body b. Bash attempts to optimize the number of times it forks when executing What you're seeing is that bash notices that it can optimize the function call by avoiding a fork when calling "test2.sh" the third time, thus the parent PID is different. This is done by the `optimize_shell_function` in builtins/evalstring.c. By the way, in the future, avoid sending scripts as screenshots and instead just send them as plain text. It makes it more difficult for us to help you because now we have to transcribe your code from a picture and hope that we didn't make any mistakes along the way.