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