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

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