1
2 #### Locals don't leak
3 f() {
4 local f_var=f_var
5 }
6 f
7 echo $f_var
8 ## stdout:
9
10 #### Globals leak
11 f() {
12 f_var=f_var
13 }
14 f
15 echo $f_var
16 ## stdout: f_var
17
18 #### Return statement
19 f() {
20 echo one
21 return 42
22 echo two
23 }
24 f
25 ## stdout: one
26 ## status: 42
27
28 #### Dynamic Scope
29 f() {
30 echo $g_var
31 }
32 g() {
33 local g_var=g_var
34 f
35 }
36 g
37 ## stdout: g_var
38
39 #### Dynamic Scope Mutation (wow this is bad)
40 f() {
41 g_var=f_mutation
42 }
43 g() {
44 local g_var=g_var
45 f
46 echo "g: $g_var"
47 }
48 g
49 ## stdout: g: f_mutation
50
51 #### Assign local separately
52 f() {
53 local f
54 f='new-value'
55 echo "[$f]"
56 }
57 f
58 ## stdout: [new-value]
59 ## status: 0
60
61 #### Assign a local and global on same line
62 myglobal=
63 f() {
64 local mylocal
65 mylocal=L myglobal=G
66 echo "[$mylocal $myglobal]"
67 }
68 f
69 echo "[$mylocal $myglobal]"
70 ## stdout-json: "[L G]\n[ G]\n"
71 ## status: 0
72
73 #### Return without args gives previous
74 f() {
75 ( exit 42 )
76 return
77 }
78 f
79 echo status=$?
80 ## STDOUT:
81 status=42
82 ## END