1 #!/usr/bin/env bash
2 #
3 # Test combination of var ops.
4 #
5 # NOTE: There are also slice tests in {array,arith-context}.test.sh.
6
7 #### String slice
8 foo=abcdefg
9 echo ${foo:1:3}
10 ## STDOUT:
11 bcd
12 ## END
13
14 #### Cannot take length of substring slice
15 # These are runtime errors, but we could make them parse time errors.
16 v=abcde
17 echo ${#v:1:3}
18 ## status: 1
19 ## OK osh status: 2
20 # zsh actually implements this!
21 ## OK zsh stdout: 3
22 ## OK zsh status: 0
23
24 #### Out of range string slice: begin
25 # out of range begin doesn't raise error in bash, but in mksh it skips the
26 # whole thing!
27 foo=abcdefg
28 echo _${foo:100:3}
29 echo $?
30 ## STDOUT:
31 _
32 0
33 ## END
34 ## BUG mksh stdout-json: "\n0\n"
35
36 #### Out of range string slice: length
37 # OK in both bash and mksh
38 foo=abcdefg
39 echo _${foo:3:100}
40 echo $?
41 ## STDOUT:
42 _defg
43 0
44 ## END
45 ## BUG mksh stdout-json: "_defg\n0\n"
46
47 #### String slice: negative begin
48 foo=abcdefg
49 echo ${foo: -4:3}
50 ## OK osh stdout:
51 ## stdout: def
52
53 #### String slice: negative second arg is position, not length
54 foo=abcdefg
55 echo ${foo:3:-1} ${foo: 3: -2} ${foo:3 :-3 }
56 ## OK osh stdout:
57 ## stdout: def de d
58 ## BUG mksh stdout: defg defg defg
59
60
61 #### strict_word_eval with string slice
62 shopt -s strict_word_eval || true
63 echo slice
64 s='abc'
65 echo -${s: -2}-
66 ## STDOUT:
67 slice
68 ## END
69 ## status: 1
70 ## N-I bash/mksh/zsh status: 0
71 ## N-I bash/mksh/zsh STDOUT:
72 slice
73 -bc-
74 ## END
75
76 #### String slice with math
77 # I think this is the $(()) language inside?
78 i=1
79 foo=abcdefg
80 echo ${foo: i+4-2 : i + 2}
81 ## stdout: def
82
83 #### Slice undefined
84 echo -${undef:1:2}-
85 set -o nounset
86 echo -${undef:1:2}-
87 echo -done-
88 ## STDOUT:
89 --
90 ## END
91 ## status: 1
92 # mksh doesn't respect nounset!
93 ## BUG mksh status: 0
94 ## BUG mksh STDOUT:
95 --
96 --
97 -done-
98 ## END
99
100 #### Slice UTF-8 String
101 # mksh slices by bytes.
102 foo='--μ--'
103 echo ${foo:1:3}
104 ## stdout: -μ-
105 ## BUG mksh stdout: -μ
106
107 #### Slice string with invalid UTF-8 results in empty string and warning
108 s=$(echo -e "\xFF")bcdef
109 echo -${s:1:3}-
110 ## status: 0
111 ## STDOUT:
112 --
113 ## END
114 ## STDERR:
115 [??? no location ???] warning: Invalid start of UTF-8 character
116 ## END
117 ## BUG bash/mksh/zsh status: 0
118 ## BUG bash/mksh/zsh STDOUT:
119 -bcd-
120 ## END
121 ## BUG bash/mksh/zsh stderr-json: ""
122
123
124 #### Slice string with invalid UTF-8 with strict_word_eval
125 shopt -s strict_word_eval || true
126 echo slice
127 s=$(echo -e "\xFF")bcdef
128 echo -${s:1:3}-
129 ## status: 1
130 ## STDOUT:
131 slice
132 ## END
133 ## N-I bash/mksh/zsh status: 0
134 ## N-I bash/mksh/zsh STDOUT:
135 slice
136 -bcd-
137 ## END
138