Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / tests / nameref.tests
1 # basic nameref tests
2 bar=one
3 flow=two
4 flip=three
5
6 foo=bar
7 typeset -n foo
8
9 typeset -n fee=flow
10
11 echo ${foo}
12 echo ${fee}
13
14 typeset -n fee=flip
15 echo ${fee}
16
17 typeset -n
18
19 echo turning off nameref attribute on foo
20 typeset +n foo=other
21 echo ${foo}
22 echo after +n foo bar = $bar
23
24 unset foo bar fee
25
26 bar=one
27
28 foo=bar
29 typeset -n foo
30
31 foo=two printf "%s\n" $foo
32 foo=two eval 'printf "%s\n" $foo'
33
34 foo=two echo $foo
35
36 unset foo bar
37 # other basic assignment tests
38 bar=one
39
40 echo "expect <one>"
41 recho ${bar}
42 typeset -n foo=bar
43 foo=two
44
45 echo "expect <two>"
46 recho ${bar}
47
48 # this appears to be a ksh93 bug; it doesn't unset foo here and messes up
49 # later
50 unset foo bar
51
52 # initial tests of working inside shell functions
53 echoval()
54 {
55         typeset -n ref=$1
56         printf "%s\n" $ref
57 }
58
59 foo=bar
60 bar=one
61 echo "expect <$foo>"
62 echoval foo
63 echo "expect <$bar>"
64 echoval bar
65
66 unset foo bar
67 changevar()
68 {
69         typeset -n v=$1
70
71         shift
72         v="$@"
73         echo "changevar: expect <$@>"
74         recho "$v"
75 }
76
77 bar=one
78
79 echo "expect <one>"
80 recho ${bar}
81 changevar bar two
82 echo "expect <two>"
83 recho $bar
84
85 changevar bar three four five
86 echo "expect <three four five>"
87 recho "$bar"
88
89 unset foo bar
90 unset -n foo bar
91 readonly foo=one
92 typeset -n bar=foo
93 bar=4
94 foo=4
95
96 echo $foo
97 echo $bar
98
99 assignvar()
100 {
101         typeset -n ref=$1
102         shift
103         ref="$@"
104 }
105
106 readonly foo=one
107
108 assignvar foo two three four
109 echo $foo
110
111 ${THIS_SH} ./nameref1.sub
112 ${THIS_SH} ./nameref2.sub
113 ${THIS_SH} ./nameref3.sub
114 ${THIS_SH} ./nameref4.sub
115 ${THIS_SH} ./nameref5.sub
116 ${THIS_SH} ./nameref6.sub
117 ${THIS_SH} ./nameref7.sub
118 ${THIS_SH} ./nameref8.sub