OILS / spec / tilde.test.sh View on Github | oilshell.org

188 lines, 88 significant
1## compare_shells: bash dash mksh zsh
2## oils_failures_allowed: 1
3
4#### ~ expansion in assignment
5HOME=/home/bob
6a=~/src
7echo $a
8## stdout: /home/bob/src
9
10#### ~ expansion in readonly assignment
11# dash fails here!
12# http://stackoverflow.com/questions/8441473/tilde-expansion-doesnt-work-when-i-logged-into-gui
13HOME=/home/bob
14readonly const=~/src
15echo $const
16## stdout: /home/bob/src
17## BUG dash stdout: ~/src
18
19#### No ~ expansion in dynamic assignment
20HOME=/home/bob
21binding='const=~/src'
22readonly "$binding"
23echo $const
24## stdout: ~/src
25
26#### No tilde expansion in word that looks like assignment but isn't
27# bash and mksh mistakenly expand here!
28# bash fixes this in POSIX mode (gah).
29# http://lists.gnu.org/archive/html/bug-bash/2016-06/msg00001.html
30HOME=/home/bob
31echo x=~
32## stdout: x=~
33## BUG bash/mksh stdout: x=/home/bob
34
35#### tilde expansion of word after redirect
36HOME=$TMP
37echo hi > ~/tilde1.txt
38cat $HOME/tilde1.txt | wc -c
39## stdout: 3
40## status: 0
41
42#### other user
43echo ~nonexistent
44## stdout: ~nonexistent
45# zsh doesn't like nonexistent
46## OK zsh stdout-json: ""
47## OK zsh status: 1
48
49#### ${undef:-~}
50HOME=/home/bar
51echo ${undef:-~}
52echo ${HOME:+~/z}
53echo "${undef:-~}"
54echo ${undef:-"~"}
55## STDOUT:
56/home/bar
57/home/bar/z
58~
59~
60## END
61
62#### ${x//~/~root}
63HOME=/home/bar
64x=~
65echo ${x//~/~root}
66
67# gah there is some expansion, what the hell
68echo ${HOME//~/~root}
69
70x=[$HOME]
71echo ${x//~/~root}
72
73## STDOUT:
74/root
75/root
76[/root]
77## END
78## N-I dash status: 2
79## N-I dash stdout-json: ""
80
81#### x=foo:~ has tilde expansion
82HOME=/home/bar
83x=foo:~
84echo $x
85echo "$x" # quotes don't matter, the expansion happens on assignment?
86x='foo:~'
87echo $x
88
89x=foo:~, # comma ruins it, must be /
90echo $x
91
92x=~:foo
93echo $x
94
95# no tilde expansion here
96echo foo:~
97## STDOUT:
98foo:/home/bar
99foo:/home/bar
100foo:~
101foo:~,
102/home/bar:foo
103foo:~
104## END
105
106#### a[x]=foo:~ has tilde expansion
107case $SH in (dash|zsh) exit ;; esac
108
109HOME=/home/bar
110declare -a a
111a[0]=foo:~
112echo ${a[0]}
113
114declare -A A
115A['x']=foo:~
116echo ${A['x']}
117
118## STDOUT:
119foo:/home/bar
120foo:/home/bar
121## END
122## N-I dash/zsh stdout-json: ""
123
124#### tilde expansion an assignment keyword
125HOME=/home/bar
126f() {
127 local x=foo:~
128 echo $x
129}
130f
131## STDOUT:
132foo:/home/bar
133## END
134## BUG dash STDOUT:
135foo:~
136## END
137
138#### x=${undef-~:~}
139HOME=/home/bar
140
141x=~:${undef-~:~}
142echo $x
143
144# Most shells agree on a different behavior, but with the OSH parsing model,
145# it's easier to agree with yash. bash disagrees in a different way
146
147## OK bash STDOUT:
148/home/bar:/home/bar:~
149## END
150## OK osh/yash STDOUT:
151/home/bar:~:~
152## END
153## STDOUT:
154/home/bar:/home/bar:/home/bar
155## END
156
157#### strict tilde
158echo ~nonexistent
159
160shopt -s strict_tilde
161echo ~nonexistent
162
163echo status=$?
164## status: 1
165## STDOUT:
166~nonexistent
167## END
168## N-I bash/dash/mksh status: 0
169## N-I bash/dash/mksh STDOUT:
170~nonexistent
171~nonexistent
172status=0
173## END
174## OK zsh stdout-json: ""
175
176#### temp assignment x=~ env
177
178HOME=/home/bar
179
180xx=~ env | grep xx=
181
182# Does it respect the colon rule too?
183xx=~root:~:~ env | grep xx=
184
185## STDOUT:
186xx=/home/bar
187xx=/root:/home/bar:/home/bar
188## END