summaryrefslogtreecommitdiff
path: root/tests/varenv23.sub
blob: 03202ad4afa068a260d034f8924d04aed01436ab (plain)
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
91
92
93
94
95
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# posix rules for assignments preceding export/readonly in functions, using
# local and global vars; default rules for assignments preceding declare/local
# in functions, always using local vars.
#
# changes to local variables should never propagate upward from the function
# to its caller, even in posix mode

x=1
x=4 declare -r x
declare -p x
y=2
y=5 readonly y
declare -p y
# can't use x and y from here on

f() { local -r a=3; echo f:$a; }
f1() { declare -r b=3; echo f1:$b; }

a=4 f
b=4 f1
echo global1:$a $b

set -o posix
a=4 f
b=4 f1
echo global2:$a $b
set +o posix

unset -f f f1

f() { local a=3; readonly a; echo f:$a; }
f1() { local b=3; declare -r b; echo f1:$b; }

a=4 f
b=4 f1
echo global:$a $b

set -o posix
a=4 f
b=4 f1
echo global:$a $b
set +o posix

unset -f f f1

f() { local a; a=3 readonly a; echo f:$a; }

a=4 f
echo $a
set -o posix
a=4 f
echo $a
set +o posix

f1() { a=3 readonly a; echo f1:$a; }

a=7 f1
echo global: $a

set -o posix
a=7 f1
echo global: $a
set +o posix

unset -f f f1
# can't use a from here on

c=7 b=8
f() { c=3 readonly c; echo f: $c; }
f1() { b=4 declare -r b; echo f1: $b; }

f
echo global: $(declare -p c)

f1
echo global: $(declare -p b)

unset -f f f1

# can't use c from here on