1 #!/usr/bin/env bash
2 #
3 # Test combination of var ops.
4
5 ### String length
6 v=foo
7 echo ${#v}
8 # stdout: 3
9
10 ### Length of undefined variable
11 echo ${#undef}
12 # stdout: 0
13
14 ### Length of undefined variable with nounset
15 set -o nounset
16 echo ${#undef}
17 # status: 1
18 # OK dash status: 2
19
20 ### Cannot take length of substring slice
21 # These are runtime errors, but we could make them parse time errors.
22 v=abcde
23 echo ${#v:1:3}
24 # status: 1
25 # N-I dash status: 0
26 # N-I dash stdout: 5
27
28 ### Pattern replacement
29 v=abcde
30 echo ${v/c*/XX}
31 # stdout: abXX
32 # N-I dash status: 2
33 # N-I dash stdout-json: ""
34
35 ### Pattern replacement ${v/} is not valid
36 v=abcde
37 echo -${v/}-
38 echo status=$?
39 # status: 2
40 # stdout-json: ""
41 # N-I dash status: 2
42 # N-I dash stdout-json: ""
43 # BUG bash/mksh status: 0
44 # BUG bash/mksh stdout-json: "-abcde-\nstatus=0\n"
45
46 ### Pattern replacement ${v//} is not valid
47 v='a/b/c'
48 echo -${v//}-
49 echo status=$?
50 # status: 2
51 # stdout-json: ""
52 # N-I dash status: 2
53 # N-I dash stdout-json: ""
54 # BUG bash/mksh status: 0
55 # BUG bash/mksh stdout-json: "-a/b/c-\nstatus=0\n"
56
57 ### String slice
58 foo=abcdefg
59 echo ${foo:1:3}
60 # stdout: bcd
61 # N-I dash status: 2
62 # N-I dash stdout-json: ""
63
64 ### Negative string slice
65 foo=abcdefg
66 echo ${foo: -4:3}
67 # stdout: def
68 # N-I dash status: 2
69 # N-I dash stdout-json: ""
70
71 ### String slice with math
72 # I think this is the $(()) language inside?
73 i=1
74 foo=abcdefg
75 echo ${foo: i-3-2 : i + 2}
76 # stdout: def
77 # N-I dash status: 2
78 # N-I dash stdout-json: ""