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

How to check the variable is set or empty in bash

A variable can be defined or undefined. When any variable is not declared or declared but no value is assigned then the...
29/12/2020

How to use Variables in Bash Programming

Variables work as temporary storage for any programming language. The coder needs to have a clear concept of using different...
28/12/2020

How to use inotify and rsync to create a live backup system using only a while loop in a bash script

Why should you use Bash Scripts to perform folder synchronizations and backups? Bash is by far the most popular and used...
28/12/2020
Bài Viết

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

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024