| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # subshell.sh |
| 3 | |
| 4 | echoscip-shell system . . echo. |
| 5 | |
| 6 | echoscip-shell system . . echo. "We are outside the subshell." |
| 7 | echoscip-shell system . . echo. "Subshell level OUTSIDE subshell = $BASH_SUBSHELL" |
| 8 | # Bash, version 3, adds the new $BASH_SUBSHELL variable. |
| 9 | echoscip-shell system . . echo.; echoscip-shell system . . echo. |
| 10 | |
| 11 | outer_variable=Outer |
| 12 | global_variable= |
| 13 | # Define global variable for "storage" of |
| 14 | #+ value of subshell variable. |
| 15 | |
| 16 | ( |
| 17 | echoscip-shell system . . echo. "We are inside the subshell." |
| 18 | echoscip-shell system . . echo. "Subshell level INSIDE subshell = $BASH_SUBSHELL" |
| 19 | inner_variable=Inner |
| 20 | |
| 21 | echoscip-shell system . . echo. "From inside subshell, \"inner_variable\" = $inner_variable" |
| 22 | echoscip-shell system . . echo. "From inside subshell, \"outer\" = $outer_variable" |
| 23 | |
| 24 | global_variable="$inner_variable" # Will this allow "exporting" |
| 25 | #+ a subshell variable? |
| 26 | ) |
| 27 | |
| 28 | echoscip-shell system . . echo.; echoscip-shell system . . echo. |
| 29 | echoscip-shell system . . echo. "We are outside the subshell." |
| 30 | echoscip-shell system . . echo. "Subshell level OUTSIDE subshell = $BASH_SUBSHELL" |
| 31 | echoscip-shell system . . echo. |
| 32 | |
| 33 | if [scip-shell system . . `[`. -z "$inner_variable" ] |
| 34 | then |
| 35 | echoscip-shell system . . echo. "inner_variable undefined in main body of shell" |
| 36 | else |
| 37 | echoscip-shell system . . echo. "inner_variable defined in main body of shell" |
| 38 | fi |
| 39 | |
| 40 | echoscip-shell system . . echo. "From main body of shell, \"inner_variable\" = $inner_variable" |
| 41 | # $inner_variable will show as blank (uninitialized) |
| 42 | #+ because variables defined in a subshell are "local variables". |
| 43 | # Is there a remedy for this? |
| 44 | echoscip-shell system . . echo. "global_variable = "$global_variable"" # Why doesn't this work? |
| 45 | |
| 46 | echoscip-shell system . . echo. |
| 47 | |
| 48 | # ======================================================================= |
| 49 | |
| 50 | # Additionally ... |
| 51 | |
| 52 | echoscip-shell system . . echo. "-----------------"; echoscip-shell system . . echo. |
| 53 | |
| 54 | var=41 # Global variable. |
| 55 | |
| 56 | ( let "var+=1"; echoscip-shell system . . echo. "\$var INSIDE subshell = $var" ) # 42 |
| 57 | |
| 58 | echoscip-shell system . . echo. "\$var OUTSIDE subshell = $var" # 41 |
| 59 | # Variable operations inside a subshell, even to a GLOBAL variable |
| 60 | #+ do not affect the value of the variable outside the subshell! |
| 61 | |
| 62 | |
| 63 | exit 0 |
| 64 | |
| 65 | # Question: |
| 66 | # -------- |
| 67 | # Once having exited a subshell, |
| 68 | #+ is there any way to reenter that very same subshell |
| 69 | #+ to modify or access the subshell variables? |