How To Use Array In Bash Script | Linux
The fact that arrays are structurally different from other variables means that some new syntax is required to manipulate arrays. Wherever possible, doing things to arrays is syntactically very similar to doing the equivalent thing to a string, but there are instances where that syntax is not flexible enough.
Arrays are a powerful feature of the shell that can be used in many different ways. They expand the power of the shell quite considerably, although requiring arrays as a supported feature restricts the portability of a shell script to systems that support arrays. Sparse arrays can be very useful, too, with none of the memory management necessary for lower-level languages such as C.
Copying an Array
Copying one array to another is simple. It is important for quoting and spacing that the ${array[@]} format (rather than ${array[*]}) is used, and also that double quotes are put around the whole construct. These demonstrations are probably the clearest way to show what can happen when other forms are used.
$ activities=( swimming “water skiing” canoeing “white-water rafting” surfing ) $ for act in ${activities[@]} do echo “Activity: $act” doneActivity: swimming
Activity: water
Activity: skiing
Activity: canoeing
Activity: white-water
Activity: rafting
Activity: surfing
$
What happened here is that because there were no double quotes around the list, “swimming,” “water,” and “skiing” were all treated as separate words. Placing double quotes around the whole thing fixes this:
$ for act in “${activities[@]}” do echo “Activity: $act” doneActivity: swimming Activity: water skiing Activity: canoeing
Activity: white-water rafting
Activity: surfing
$
Similarly, the * is not suitable either with or without quotes. Without quotes, it does the same as the
@ symbol. With quotes, the whole array is boiled down into a single string.
$ for act in ${activities[*]} do echo “Activity: $act” doneActivity: swimming
Activity: water
Activity: skiing
Activity: canoeing
Activity: white-water
Activity: rafting
Activity: surfing
$ for act in “${activities[*]}” do echo “Activity: $act” doneActivity: swimming water skiing canoeing white-water rafting surfing
$
Thus, to copy an array, define a new array with the values of “${activities[@]}”. This will pre- serve whitespace in the same way as the for loop in the preceding code got the correct treatment of whitespace. This is shown in the following code.
$ hobbies=( “${activities[@]}” ) $ for hobby in “${hobbies[@]}” do echo “Hobby: $hobby” doneHobby: swimming
Hobby: water skiing
Hobby: canoeing
Hobby: white-water rafting
Hobby: surfing
$
This does not work properly for sparse arrays, however. The actual value of the index is not passed on in this way, so the hobbies array cannot be a true copy of the activities array.
$ activities[10]=”scuba diving” $ hobbies=”( ${activities[@]} )” $ for act in `seq 0 10` do echo “$act : ${activities[$act]} / ${hobbies[$act]}” done0 : swimming / swimming
1 : water skiing / water skiing
2 : canoeing / canoeing
3 : white-water rafting / white-water rafting
4 : surfing / surfing
5 : / scuba diving
6 : /
7 : /
8 : /
9 : /
10 : scuba diving /
$
Appending to an array
Appending to an array is much the same as copying it. The simplest way to append to an array is to extend the syntax for copying an array.
$ hobbies=( “${activities[@]” diving ) $ for hobby in “${hobbies[@]}” do echo “Hobby: $hobby” doneHobby: swimming
Hobby: water skiing
Hobby: canoeing
Hobby: white-water rafting
Hobby: surfing Hobby: scuba diving Hobby: diving
$
Earlier in this chapter you saw how seq 0 $((${#beatles[@]} – 1)) was used to get the final actual element of the array. The fact that the array is indexed from zero made this task a little bit awkward. When appending a single item to the array, the fact that the array is zero-indexed actually makes it easier.
$ hobbies[${#hobbies[@]}]=rowing $ for hobby in “${hobbies[@]}” do echo “Hobby: $hobby” doneHobby: swimming
Hobby: water skiing
Hobby: canoeing
Hobby: white-water rafting
Hobby: surfing
Hobby: scuba diving
Hobby: diving
Hobby: rowing
$
The bash shell does have a builtin syntax to combine two arrays. With the C-like notation of +=, this method is concise and allows for very clear code.
$ airsports=( flying gliding parachuting ) $ activities+=(“${airsports[@]}”) $ for act in “${activities[@]}” do echo “Activity: $act” doneActivity: swimming
Activity: water skiing
Activity: canoeing
Activity: white-water rafting
Activity: surfing
Activity: scuba diving
Activity: climbing
Activity: walking
Activity: cycling
Activity: flying
Activity: gliding
Activity: parachuting
$
Deleting from an array
Deleting an item from an array is the same as deleting a variable; you can use myarray[3]= or unset myarray[3]. Similarly, you can unset the whole array. However, myarray= by itself will only clear the value of the first item in the array. All of these situations are demonstrated in the code that follows.
$ for act in `seq 0 $((${#activities[@]} – 1))` do echo “Activity $act: ${activities[$act]}” doneActivity 0: swimming Activity 1: water skiing Activity 2: canoeing
Activity 3: white-water rafting
Activity 4: surfing
Activity 5: scuba diving
Activity 6: climbing
Activity 7: walking
Activity 8: cycling
Activity 9: flying
Activity 10: gliding
Activity 11: parachuting
$ activities[7]= $ for act in `seq 0 $((${#activities[@]} – 1))` do echo “Activity $act: ${activities[$act]}” doneActivity 0: swimming
Activity 1: water skiing
Activity 2: canoeing
Activity 3: white-water rafting
Activity 4: surfing
Activity 5: scuba diving
Activity 6: climbing
Activity 7:
Activity 8: cycling
Activity 9: flying
Activity 10: gliding
Activity 11: parachuting
$
The effect of this is to make a sparse array. Using unset activities[7] has largely the same effect. There is a difference between setting a variable to the empty string and unsetting it entirely, but it is obvious only when the ${variable+string} or
${variable?string} forms are used. $ echo ${activities[7] $ echo ${activities[7]+”Item 7 is set”} Item 7 is set $ unset activities[7] $ echo ${activities[7]+”Item 7 is set”} $Once more, references to the array without an index are interpreted as references to the first element in the array. So clearing the array in this way only removes the first item.
$ activities= $ for act in `seq 0 $((${#activities[@]} – 1))` do echo “Activity $act: ${activities[$act]}” doneActivity 0:
Activity 1: water skiing
Activity 2: canoeing
Activity 3: white-water rafting
Activity 4: surfing
Activity 5: scuba diving Activity
6: climbing
Activity 7:
Activity 8: cycling
Activity 9: flying
Activity 10: gliding
Activity 11: parachuting
If you unset the activities array itself, the whole thing disappears. This is the correct way to unset an array, although you can also use unset myarray[*].
$ unset activities $ for act in `seq 0 $((${#activities[@]} – 1))` do echo “Activity $act: ${activities[$act]}” done$
In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.