forked from sandervanvugt/bash-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array1
executable file
·42 lines (32 loc) · 791 Bytes
/
array1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
my_array=( a b c )
# print index value 1
echo ${my_array[1]}
# print all items in the array
echo ${my_array[@]}
echo ${my_array[*]}
# print all index values and not their value
echo ${!my_array[@]}
# print the length of the array
echo ${#my_array[@]}
# loop over all items in the array; printing all keys as well as all values
for i in "${!my_array[@]}"
do
echo "$i" "${my_array[$i]}"
done
# loop on just the values and not the keys
for i in "${my_array[@]}"
do
echo "$i"
done
# adding a value at a specific position
# using 9 to make sure it is last
my_array[9]=d
echo ${my_array[@]}
echo ${my_array[9]}
# adding items to the end of the array, using the first available index
my_array+=( e f )
for i in "${!my_array[@]}"
do
echo "$i" "${my_array[$i]}"
done