| 1 | #!/usr/bin/env bash
 | 
| 2 | 
 | 
| 3 | 
 | 
| 4 | sleep_test() {
 | 
| 5 |   echo "Sleeping for 10 seconds in subshell of PID $$"
 | 
| 6 |   type sleep  # external command
 | 
| 7 | 
 | 
| 8 |   # NOTE: Within a subshell, $$ returns the PID of the script, not the subshell!
 | 
| 9 | 
 | 
| 10 |   # process tree for dash and bash looks different.
 | 
| 11 |   #
 | 
| 12 |   # dash appears to run the first thing not in a subshell?  Hm maybe I could do
 | 
| 13 |   # that too.  But then global var references would be different.
 | 
| 14 | 
 | 
| 15 |   ( echo "PID $$"; sleep 10 ) | tee foo.txt
 | 
| 16 |   #( echo "PID $BASHPID"; sleep 10 )
 | 
| 17 | 
 | 
| 18 |   # This doesn't cause an extra subshell in bash.
 | 
| 19 |   ( sleep 10 )
 | 
| 20 | }
 | 
| 21 | 
 | 
| 22 | g=0
 | 
| 23 | 
 | 
| 24 | myfunc() {
 | 
| 25 |   echo 'running myfunc'
 | 
| 26 |   g=1
 | 
| 27 | }
 | 
| 28 | 
 | 
| 29 | # Hm bash and dash both seem to behave the same here.
 | 
| 30 | var_test() {
 | 
| 31 |   myfunc | tee _tmp/command-sub.txt
 | 
| 32 |   { g=2; echo brace; } | tee _tmp/command-sub.txt
 | 
| 33 | 
 | 
| 34 |   echo "g after pipeline: $g"
 | 
| 35 | }
 | 
| 36 | 
 | 
| 37 | #sleep_test "$@"
 | 
| 38 | 
 | 
| 39 | var_test
 |