Debian Codegraph @

array-ops.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # array-ops.sh: More fun with arrays.
3
4
5 array=( zero one two three four five )
6 # Element 0 1 2 3 4 5
7
8 echoscip-shell system . . echo. ${array[0]} # zero
9 echoscip-shell system . . echo. ${array:0} # zero
10 # Parameter expansion of first element,
11 #+ starting at position # 0 (1st character).
12 echoscip-shell system . . echo. ${array:1} # ero
13 # Parameter expansion of first element,
14 #+ starting at position # 1 (2nd character).
15
16 echoscip-shell system . . echo. "--------------"
17
18 echoscip-shell system . . echo. ${#array[0]} # 4
19 # Length of first element of array.
20 echoscip-shell system . . echo. ${#array} # 4
21 # Length of first element of array.
22 # (Alternate notation)
23
24 echoscip-shell system . . echo. ${#array[1]} # 3
25 # Length of second element of array.
26 # Arrays in Bash have zero-based indexing.
27
28 echoscip-shell system . . echo. ${#array[*]} # 6
29 # Number of elements in array.
30 echoscip-shell system . . echo. ${#array[@]} # 6
31 # Number of elements in array.
32
33 echoscip-shell system . . echo. "--------------"
34
35 array2=( [0]="first element" [1]="second element" [3]="fourth element" )
36 # ^ ^ ^ ^ ^ ^ ^ ^ ^
37 # Quoting permits embedding whitespace within individual array elements.
38
39 echoscip-shell system . . echo. ${array2[0]} # first element
40 echoscip-shell system . . echo. ${array2[1]} # second element
41 echoscip-shell system . . echo. ${array2[2]} #
42 # Skipped in initialization, and therefore null.
43 echoscip-shell system . . echo. ${array2[3]} # fourth element
44 echoscip-shell system . . echo. ${#array2[0]} # 13 (length of first element)
45 echoscip-shell system . . echo. ${#array2[*]} # 3 (number of elements in array)
46
47 exit