Bash-4.2 distribution sources and documentation
[platform/upstream/bash.git] / tests / func.tests
1 a()
2 {
3         x=$((x - 1))
4         return 5
5 }
6
7 b()
8 {
9         x=$((x - 1))
10         a
11         echo a returns $?
12         return 4
13 }
14
15 c()
16 {
17         x=$((x - 1))
18         b
19         echo b returns $?
20         return 3
21 }
22
23 d()
24 {
25         x=$((x - 1))
26         c
27         echo c returns $?
28         return 2
29 }
30
31 e()
32 {
33         d
34         echo d returns $?
35         echo in e
36         x=$((x - 1))
37         return $x
38 }
39
40 f()
41 {
42         e
43         echo e returned $?
44         echo x is $x
45         return 0
46 }
47
48 x=30
49 f
50
51 # make sure unsetting a local variable preserves the `local' attribute
52 f1()
53 {
54         local zz
55         zz=abcde
56         echo $zz
57         unset zz
58         zz=defghi
59         echo $zz
60 }
61
62 zz=ZZ
63 echo $zz
64 f1
65 echo $zz
66
67 unset -f f1
68 f1()
69 {
70         return 5
71 }
72
73 ( f1 )
74 echo $?
75
76 unset -f f1
77 f1()
78 {
79         sleep 5
80         return 5
81 }
82
83 f1 &
84 wait
85 echo $?
86
87 unset -f f1
88
89 f1()
90 {
91         echo $AVAR
92         printenv AVAR
93 }
94
95 AVAR=AVAR
96 echo $AVAR
97 f1
98 AVAR=foo f1
99 echo $AVAR
100
101 unset -f f1
102 # make sure subshells can do a `return' if we're executing in a function
103 f1()
104 {
105         ( return 5 )
106         status=$?
107         echo $status
108         return $status
109 }
110
111 f1
112 echo $?
113
114 declare -F f1   # should print just the name
115 declare -f f1   # should print the definition, too
116
117 # no functions should be exported, right?
118 declare -xF
119 declare -xf
120
121 # FUNCNAME tests
122 func2()
123 {
124         echo FUNCNAME = $FUNCNAME
125 }
126
127 func()
128 {
129         echo before: FUNCNAME = $FUNCNAME
130         func2
131         echo after: FUNCNAME = $FUNCNAME
132 }
133
134 echo before: try to assign to FUNCNAME
135 FUNCNAME=7
136
137 echo outside: FUNCNAME = $FUNCNAME
138 func
139 echo outside2: FUNCNAME = $FUNCNAME
140
141 # test exported functions (and cached exportstr)
142 zf()
143 {
144         echo this is zf
145 }
146 export -f zf
147
148 ${THIS_SH} -c 'type -t zf'
149 ${THIS_SH} -c 'type zf'
150
151 ${THIS_SH} ./func1.sub
152
153 # tests for functions whose bodies are not group commands, with and without
154 # attached redirections
155 ${THIS_SH} ./func2.sub
156
157 # test for some posix-specific function behavior
158 ${THIS_SH} ./func3.sub
159
160 # FUNCNEST testing
161 ${THIS_SH} ./func4.sub
162
163 unset -f myfunction
164 myfunction() {
165     echo "bad shell function redirection"
166 } >> /dev/null
167
168 myfunction
169 myfunction | cat
170
171 segv()
172 {
173         echo foo | return 5
174 }
175
176 segv
177 echo $?
178
179 exit 0