Debian Codegraph @

neg-offset.sh

raw symbols SCIP
1 #!/bin/bash
2 # Bash, version -ge 4.2
3 # Negative length-index in substring extraction.
4 # Important: It changes the interpretation of this construct!
5
6 stringZ=abcABC123ABCabc
7
8 echo ${stringZ} # abcABC123ABCabc
9 # Position within string: 0123456789.....
10 echo ${stringZ:2:3} # cAB
11 # Count 2 chars forward from string beginning, and extract 3 chars.
12 # ${string:position:length}
13
14 # So far, nothing new, but now ...
15
16 # abcABC123ABCabc
17 # Position within string: 0123....6543210
18 echo ${stringZ:3:-6} # ABC123
19 # ^
20 # Index 3 chars forward from beginning and 6 chars backward from end,
21 #+ and extract everything in between.
22 # ${string:offset-from-front:offset-from-end}
23 # When the "length" parameter is negative,
24 #+ it serves as an offset-from-end parameter.
25
26 # See also neg-array.sh.