How to Declare Array in Shell Scripting?

//

Scott Campbell

Shell scripting is a powerful tool for automating tasks and managing system configurations. One of the fundamental concepts in shell scripting is working with arrays.

An array is a collection of elements that can store multiple values under a single variable name. In this tutorial, we will learn how to declare arrays in shell scripting and perform various operations on them.

Declaring an Array

To declare an array in shell scripting, you can use the following syntax:

array_name=(value1 value2 value3 ..)

The array_name is the name you choose for your array, and value1, value2, value3, etc., are the values you want to assign to the array elements. You can assign any type of value to an array element, including strings, numbers, or even other variables.

Example:

sports=("Football" "Basketball" "Tennis")

In this example, we have declared an array named sports. It contains three elements: “Football”, “Basketball”, and “Tennis”.

Accessing Array Elements

To access individual elements of an array, you need to use the following syntax:

${array_name[index]}
  • array_name: The name of the array you want to access.
  • index: The index number of the element you want to retrieve. Note that array indexing starts from 0.

Example:

echo ${sports[0]}

This will output the first element of the sports array, which is “Football”.

Array Operations

Shell scripting provides various operations to manipulate arrays. Here are some commonly used operations:

1. Length of an Array

To get the length of an array, you can use the following syntax:

${#array_name[@]}

Example:

length=${#sports[@]}
echo "The length of the array is: $length"

This will output the length of the sports array.

2. Adding Elements to an Array

To add elements to an existing array, you can use the following syntax:

array_name+=(new_value)

Example:

sports+=( "Cricket" )

This will add “Cricket” as a new element to the end of the sports array.

3. Removing Elements from an Array

To remove elements from an array, you can use various techniques like unset or slice assignment. Here’s an example using unset:

unset array_name[index]

Example:

unset sports[2]

This will remove the element at index 2 from the sports array.

4. Looping through an Array

To loop through all elements of an array, you can use a for loop combined with the length of the array:

for (( i=0; i<$length; i++ ))
do
  echo ${sports[i]}
done

This will output each element of the sports array on a new line.

Conclusion

In this tutorial, we have learned how to declare arrays in shell scripting and perform various operations on them. Arrays are a powerful feature in shell scripting that allow you to store and manipulate multiple values under a single variable name. By using the proper syntax and understanding the array operations, you can efficiently work with arrays in your shell scripts.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy