How to use arrays in Bash

28/12/2020
When you want to use multiple data using single variable in any programming language then you have to use array variables. The list of data can be assigned and used using array variable. Bash is a weakly typed language that does not require to define any data type for declaring variable. Array declaration in bash is little bit different from other standard programming languages. Two types of array can be declared in bash.  Numeric array and associative array. If the index of an array is numeric then it is called numeric array and if the index of an array is string then it is called associative array. How you can declare numeric array, associative array and iterate elements of array using for loop are described with examples in this tutorial.

Example-1: Numeric Array Declaration:

The default index of any array is numeric and all values are taken as string value. A simple numeric array of 5 string values are declared here. echo command is used here to print each array values separately. If you want to print all values of array by single echo command then “*” symbol has to use in the index of the array. These two options are shown in the following example.

#!/bin/bash
MyArray=( HTML Javascript CSS JQuery Bootstrap )
 
# Print 5 values individually
 
echo "———-Print 5 values individually—————"
echo ${MyArray[0]}
echo ${MyArray[1]}
echo ${MyArray[2]}
echo ${MyArray[3]}
echo ${MyArray[4]}
 
#Print all values by using *
echo "—————–Print all values——————-"
echo ${MyArray[*]}

Output:

Example-2: Associative Array Declaration:

Each index of the array needs to be declared separately in associative array. An associative array of 4 elements is declared in the following examples. You can read the values of the each index separately like previous example by defining the index value. You can print only indexes of associative array by using “!” and “@” symbol.

#!/bin/bash
 
# Associative array declaration
declare -A MyArr
 
# Value Initialization
MyArr=( [mark]=79 [john]=93 [ella]=87 [mila]=83 )
 
# Print values
echo ${MyArr[mark]}
echo ${MyArr[john]}
echo ${MyArr[ella]}
echo ${MyArr[mila]}
 
#Print indexes
 
echo ${!MyArr[@]}

Output:

Example-3: Reading Array values using for loop:

You can easily count the total number of elements of any bash array by using “#” and “*” symbol which is shown in the first part of the following example. For loop is commonly used to iterate the values of any array. You can also read array values and array indexes separately by using for loops. Different for loops are used in the following example to read array indexes, array values and both.

#!/bin/bash
 
# Associative array declaration
declare -A MyArr
 
# Value Initialization
MyArr=( [os]=Windows [web]=PHP [db]=Oracle )
 
echo "Total number of elements=${#MyArr[*]}"
 
echo "Array values are"
 
for value in ${MyArr[@]}
do
echo $value
done
 
echo "Array indexes are"
 
for key in ${!MyArr[@]}
do
echo $key
done
 
echo "Array values and indexes:"
for key in ${!MyArr[*]}
do
echo "$key => ${MyArr[$key]}"
done

Output:

Video of this lesson here:

There are many uses of array in programming. Some common and very simple uses of array in bash are shown in this tutorial. After exercising the above examples your basic concept of bash array will be cleared and you will be able to use bash array appropriately in your script.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Arithmetic Operations

Doing arithmetic operations in bash is not similar to other standard programming languages. One of the limitations of bash...
29/12/2020

How to Execute Commands from Within a Shell Script

In bash, executing commands from a shell script can be a little intimidating at first and may require a leap of faith....
29/12/2020

Bash Environment Variables Tutorial

Overview Whenever we talk to a remote server or do anything on a shell, the shell stores data in form of variables which...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024