Debian Codegraph @

subshell.sh

raw symbols SCIP
1 #!/bin/bash
2 # subshell.sh
3
4 echo
5
6 echo "We are outside the subshell."
7 echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
8 # Bash, version 3, adds the new $BASH_SUBSHELL variable.
9 echo; echo
10
11 outer_variable=Outer
12 global_variable=
13 # Define global variable for "storage" of
14 #+ value of subshell variable.
15
16 (
17 echo "We are inside the subshell."
18 echo "Subshell level INSIDE subshell = $BASH_SUBSHELL"
19 inner_variable=Inner
20
21 echo "From inside subshell, \"inner_variable\" = $inner_variable"
22 echo "From inside subshell, \"outer\" = $outer_variable"
23
24 global_variable="$inner_variable" # Will this allow "exporting"
25 #+ a subshell variable?
26 )
27
28 echo; echo
29 echo "We are outside the subshell."
30 echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
31 echo
32
33 if [ -z "$inner_variable" ]
34 then
35 echo "inner_variable undefined in main body of shell"
36 else
37 echo "inner_variable defined in main body of shell"
38 fi
39
40 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 echo "global_variable = "$global_variable"" # Why doesn't this work?
45
46 echo
47
48 # =======================================================================
49
50 # Additionally ...
51
52 echo "-----------------"; echo
53
54 var=41 # Global variable.
55
56 ( let "var+=1"; echo "\$var INSIDE subshell = $var" ) # 42
57
58 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?