-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
23-subshells.sh
90 lines (72 loc) · 2.69 KB
/
23-subshells.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
echo "
##################################################
## Example 23.1: #
## cd in a subshell doesn't cd in the main shell #
##################################################
"
(
cd files
echo "directory in subshell: $(pwd)"
)
echo "directory in main shell: $(pwd)"
echo "
#############################################################
## Example 23.2: #
## variables set in a subshell aren't set in the main shell #
#############################################################
"
(
animal="banana slug"
echo "in subshell: animal=$animal"
)
echo "in main shell: animal="
echo "
####################################################################################
## Example 23.3: #
## pipes create a subshell #
## (another example of 'variables set in a subshell aren't set in the main shell') #
####################################################################################
"
# here's an example of a place where there's a subshell created in a pretty
# nonobvious way.
# let's try to read 'hi' into a variable.
echo hi | read -r x
echo "in main shell: x=$x"
# it didn't work! this is because `read -r x` runs in a subshell
# if we put the `echo` in the same subshell as the `read`, though, it works:
echo hi | (
read -r x
echo "in subshell: x=$x"
)
echo "
#################################################
## Example 23.4: #
## a subshell created with process substitution #
#################################################
"
function1() {
echo 'hi there!'
echo "I'm function 1!"
}
function2() {
echo 'hi there!'
echo "I'm function 2!"
}
# this runs both `function1` and `function2` in 2 different subshells, creates
# a named pipe with each one's output, and then passes the filenames of the 2
# named pipes to `diff`.
# I don't know if I've ever used this but it's cool that you can do it!
diff <(function1) <(function2)
echo "
########################################################################
## Example 23.5: #
## subshells inherit shell variables (but other child processes don't) #
########################################################################
"
ANIMAL=panda
(
echo "in subshell: ANIMAL=$ANIMAL"
)
bash -c 'echo "in this other shell: ANIMAL=$ANIMAL "'
# subshells copy all the variables from their parent shell, but other child
# shell processes, even though both of them are child processes,