Shell Scripting – Array
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed IO redirection in shell.
https://cloudaffaire.com/shell-scripting-redirections/
In this blog post, we will discuss array in shell. Like any other programming language, bash also supports the use of an array. An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called a one-dimensional array. Bash only supports a one-dimensional array.
Shell Scripting – Array:
Array declaration and initialization:
In bash, an array can be declared using () or using an array index number. An array can hold all the supported data types available in bash and you can also mix different data types in a single array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
############################# ## Shell Scripting | Array ## ############################# ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ## ---------------------------------- ## Array Declaration & Initialization ## ---------------------------------- ## Direct initialization using () myintarray=(1 2 3) ## Or using decalre -a declare -a myintarray=(1 2 3) ## Or using individual array index myintarray[0]=1 myintarray[1]=2 myintarray[2]=3 ## Array can hold mix datatypes as well (all are treated as string) mymixarray=(1 "two" 'c' d 'five' six) |
Array Index:
Array index starts with zero representing the first element of the array and is incremented by one for each subsequent array element. You can get a specific array element using its associated index value.
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 |
## ----------- ## Array Index ## ----------- mystrarray=("one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten") ## Get all items of an array echo ${mystrarray[*]} ## returns one two three four five six seven eight nine ten ## Get a specific item from the array echo ${mystrarray[2]} ## returns three ## Get all indexes of an array echo ${!mystrarray[*]} ## returns 0 1 2 3 4 5 6 7 8 9 ## Get the number of items in an array echo ${#mystrarray[@]} ## returns 10 echo ${#mystrarray[*]} ## returns 10 ## Get the length of a specific array item echo ${#mystrarray[2]} ## returns 5 ## Get some items of an array using offset and length echo ${mystrarray[@]:5:3} ## returns six seven eight ## Get some data of a particlar item in an array using offset and lenght echo ${mystrarray[2]:2:3} ## returns ree from three |
Array Operations:
You can add, substitute, remove or concatenate array elements.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
## ---------------- ## Array Operations ## ---------------- mystrarray=("three" "four" "five") ## Retrive items from an array for i in "${mystrarray[@]}" do echo "$i" ## returns three four five done echo ${mystrarray[*]} ## returns three four five echo ${mystrarray[@]} ## returns three four five echo ${mystrarray[0]} ## returns three ## Add new items into an existing array (at the end) mystrarray=("${mystrarray[@]}" "six" "eight") echo ${mystrarray[*]} ## returns one two three four five six eight ## Add new items into an existing array (at the begining) mystrarray=("one" "two" "${mystrarray[@]}") echo ${mystrarray[*]} ## returns one two three four five six eight ## Add new item into an existing array (at specific location) mystrarray=("${mystrarray[@]:0:6}" "svn" "${mystrarray[@]:6}") echo ${mystrarray[*]} ## returns one two three four five six svn eight ## Replace an existing item from an array (by pattern matching) mystrarray=(${mystrarray[@]/svn/seven}) echo ${mystrarray[*]} ## returns one two three four five six seven eight ## Replace an existing item from an array (by index position) mystrarray[6]="svn" echo ${mystrarray[*]} ## returns one two three four five six svn eight ## Delete an existing item from an array (by pattern matching) mystrarray=(${mystrarray[@]/svn/}) echo ${mystrarray[*]} ## returns one two three four five six eight ## Delete an existing item from an array (by index position) unset mystrarray[6] echo ${mystrarray[*]} ## returns one two three four five six ## Copy an arry to a new array mynewstrarray=(${mystrarray[@]}) echo ${mynewstrarray[*]} ## returns one two three four five six ## Copy part of an array to a new array unset mynewstrarray ## removes the entire array mynewstrarray mynewstrarray=(${mystrarray[@]:1:3}) echo ${mynewstrarray[*]} ## returns two three four ## Concatenate two array myintarray=(1 2 3 4 5 6) myarray=("${myintarray[@]}" "${mystrarray[@]}") echo ${myarray[*]} ## returns 1 2 3 4 5 6 one two three four five six unset myarray ## Create an array using command substitution myarray=$(ls /) echo ${myarray[*]} ## returns the output of ls / unset myarray |
Hope you have enjoyed this article. In the next blog post, we will discuss functions.