1 #!/bin/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 ### String slice
36 foo=abcdefg
37 echo ${foo:1:3}
38 # stdout: bcd
39 # N-I dash status: 2
40 # N-I dash stdout-json: ""
41
42 ### Negative string slice
43 foo=abcdefg
44 echo ${foo: -4:3}
45 # stdout: def
46 # N-I dash status: 2
47 # N-I dash stdout-json: ""
48
49 ### String slice with math
50 # I think this is the $(()) language inside?
51 i=1
52 foo=abcdefg
53 echo ${foo: i-3-2 : i + 2}
54 # stdout: def
55 # N-I dash status: 2
56 # N-I dash stdout-json: ""