Bash Essentials Series #5: Using Arrays in Bash


[*]

In the previous part of the series, you learned about variables. Variables can only contain one value.

Arrays can have multiple values ​​in them. This makes things easier when you have to deal with several variables at once. You don’t have to store individual values ​​in a new variable.

So, instead of declaring five variables like this:

distro1=Ubuntu
distro2=Fedora
distro3=SUSE
distro4=Arch Linux
distro5=Nix

You can initialize them all in one array:

distros=(Ubuntu Fedora SUSE "Arch Linux" Nix)

Unlike some other programming languages, you cannot use commas as separators for array elements.

This is good. Let’s see how to access the elements of the array.

Accessing array elements in bash

Array elements are accessed using the index (the position in the array). To access the array element at index N, use:

${array_name[N]}

πŸ’‘

Like most other programming languages, the array starts at index 0 in the Bash shell. This means that the first element has an index of 0, and the second element has an index of 1 and nth An item has an index n-1.

So, if you want to print SUSE, you’d use:

echo ${distros[2]}
Example of accessing array elements in bash shell

🚧

There should not be any white space after that ${ or before }. You can’t use it like ${ array[n] }.

Access all elements of the array at once

Let’s say you want to print all the elements of an array.

You can use echo ${ array[n]} one by one but that’s not really necessary. There is a better and easier way:

${array[*]}

This will give you all the elements of the array.

Access all array elements at once in bash shell

Get the length of the array in bash

How do you know how many elements are in an array? There is a dedicated method for Get the length of the array in bash:

${#array_name[@]}

This is very simple, isn’t it?

Get the length of the array in bash

Add array elements in bash

If you have to add additional elements to an array, use += operator l Appending an element to an existing array in bash:

array_name+=("new_value")

This is an example:

Appends a new element to the array

🚧

It is important to use () while appending an item.

You can also use the index to set the element to any position.

array_name[N]=new_value

But remember to use the correct index number. If you use it on an existing index, the new value will replace the element.

If you use an “out of range” index, it will still be added after the last element. For example, if the array is six in length and you try to assign a new value at index 9, the element will still be added as the last element in seventh position (index 6).

Delete an array element

you can use unset shell builtin to remove an array element by providing the index number:

unset array_name[N]

Here is an example, where I delete the fourth element of the array.

Delete array element in bash

You can also delete the entire array without justification:

unset array_name

πŸ’‘

There are no strict data type rules in Bash. You can create an array that contains both integers and strings.

πŸ‹οΈ Workout time

Let’s practice what you learned about bash arrays.

Exercise 1: Create a bash script containing a collection of the top five Linux distributions. Print them all.

Now, replace the middle option with Hannah Montanna Linux.

exercise 2: Create a bash script that accepts three numbers from the user and then prints them out in reverse order.

expected output:

Enter three numbers and press enter
12 23 44
Numbers in reverse order are: 44 23 12

I hope you enjoyed learning bash shell programming with this series. In the next chapter, you will learn about using if-else. Stay tuned.

[*]
[*]Source link

Leave a Comment

Your email address will not be published. Required fields are marked *