1 ## compare_shells: bash dash mksh zsh
2 ## oils_failures_allowed: 1
3
4 #### ~ expansion in assignment
5 HOME=/home/bob
6 a=~/src
7 echo $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
13 HOME=/home/bob
14 readonly const=~/src
15 echo $const
16 ## stdout: /home/bob/src
17 ## BUG dash stdout: ~/src
18
19 #### No ~ expansion in dynamic assignment
20 HOME=/home/bob
21 binding='const=~/src'
22 readonly "$binding"
23 echo $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
30 HOME=/home/bob
31 echo x=~
32 ## stdout: x=~
33 ## BUG bash/mksh stdout: x=/home/bob
34
35 #### tilde expansion of word after redirect
36 HOME=$TMP
37 echo hi > ~/tilde1.txt
38 cat $HOME/tilde1.txt | wc -c
39 ## stdout: 3
40 ## status: 0
41
42 #### other user
43 echo ~nonexistent
44 ## stdout: ~nonexistent
45 # zsh doesn't like nonexistent
46 ## OK zsh stdout-json: ""
47 ## OK zsh status: 1
48
49 #### ${undef:-~}
50 HOME=/home/bar
51 echo ${undef:-~}
52 echo ${HOME:+~/z}
53 echo "${undef:-~}"
54 echo ${undef:-"~"}
55 ## STDOUT:
56 /home/bar
57 /home/bar/z
58 ~
59 ~
60 ## END
61
62 #### ${x//~/~root}
63 HOME=/home/bar
64 x=~
65 echo ${x//~/~root}
66
67 # gah there is some expansion, what the hell
68 echo ${HOME//~/~root}
69
70 x=[$HOME]
71 echo ${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
82 HOME=/home/bar
83 x=foo:~
84 echo $x
85 echo "$x" # quotes don't matter, the expansion happens on assignment?
86 x='foo:~'
87 echo $x
88
89 x=foo:~, # comma ruins it, must be /
90 echo $x
91
92 x=~:foo
93 echo $x
94
95 # no tilde expansion here
96 echo foo:~
97 ## STDOUT:
98 foo:/home/bar
99 foo:/home/bar
100 foo:~
101 foo:~,
102 /home/bar:foo
103 foo:~
104 ## END
105
106 #### a[x]=foo:~ has tilde expansion
107 case $SH in (dash|zsh) exit ;; esac
108
109 HOME=/home/bar
110 declare -a a
111 a[0]=foo:~
112 echo ${a[0]}
113
114 declare -A A
115 A['x']=foo:~
116 echo ${A['x']}
117
118 ## STDOUT:
119 foo:/home/bar
120 foo:/home/bar
121 ## END
122 ## N-I dash/zsh stdout-json: ""
123
124 #### tilde expansion an assignment keyword
125 HOME=/home/bar
126 f() {
127 local x=foo:~
128 echo $x
129 }
130 f
131 ## STDOUT:
132 foo:/home/bar
133 ## END
134 ## BUG dash STDOUT:
135 foo:~
136 ## END
137
138 #### x=${undef-~:~}
139 HOME=/home/bar
140
141 x=~:${undef-~:~}
142 echo $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
158 echo ~nonexistent
159
160 shopt -s strict_tilde
161 echo ~nonexistent
162
163 echo 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
172 status=0
173 ## END
174 ## OK zsh stdout-json: ""
175
176 #### temp assignment x=~ env
177
178 HOME=/home/bar
179
180 xx=~ env | grep xx=
181
182 # Does it respect the colon rule too?
183 xx=~root:~:~ env | grep xx=
184
185 ## STDOUT:
186 xx=/home/bar
187 xx=/root:/home/bar:/home/bar
188 ## END