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