Summary of Creating and Using Array Methods in Bash Scripts

Summary of Creating and Using Array Methods in Bash Scripts

Defining an array in Bash

There are two ways to create new arrays in bash scripts. The first is to use the declare command to define an Array. This command will define an associative array named test_array.

$ declare -a test_array

Arrays can also be created by assigning elements.

$ test_array=(apple orange lemon)

Accessing array elements

Similar to other programming languages, bash array elements can be accessed using index numbers starting from 0 and then 1, 2, 3, ... n. This also works for associative arrays where the index numbers are numeric.

$ echo ${test_array[0]}

apple

Use @ or * instead of a specific index number to print all elements of an array.

$ echo ${test_array[@]}

apple orange lemon

Looping through an array

You can also access array elements using loops in bash scripting. Loops are very useful to go through all the array elements one by one and perform some operation on them.

for i in ${test_array[@]}

do

echo $i

don

Adding new elements to an array

You can use the (+=) operator to add any number of elements to an existing array. Just add new elements, like:

$ test_array+=(mango banana)

View the array elements after adding new:

$ echo ${test_array[@]}

apple orange lemon mango banana

Update array elements

To update array elements, just assign any new value to the existing array by index. Let's change the current array element at index 2 with grapes.

$ test_array[2]=grapes

View array elements after adding a new element:

$ echo ${test_array[@]}

apple orange grapes mango banana

Deleting an array element

Any array element can be simply deleted using its index number. Following is to remove the element at index 2 from an array in bash script.

$ unset test_array [2]

View array elements after adding a new element:

$ echo ${test_array[@]}

apple orange mango banana

You may also be interested in:
  • Bash script enables you to view Linux system information every time you log in to the shell
  • How to execute Linux Bash commands in Python3
  • Detailed explanation of how to pass password to ssh/scp command in bash script
  • A Deep Understanding of Angle Brackets in Bash (For Beginners)
  • Detailed explanation of bash command usage
  • Implementation of Java code executing shell commands
  • Java simple implementation of calling command line and getting execution result example
  • Example of calling shell command in java and getting the execution result
  • Implementing bash command process parsing through Java

<<:  MySQL database development specifications [recommended]

>>:  Understanding and using React useEffect

Recommend

Tutorial diagram of installing zabbix2.4 under centos6.5

The fixed IP address of the centos-DVD1 version s...

Problems and solutions of using jsx syntax in React-vscode

Problem Description After installing the plugin E...

HTML page jump and parameter transfer issues

HTML page jump: window.open(url, "", &q...

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...

HTML background color gradient effect achieved through CSS style

Effect screenshots: Implementation code: Copy code...

Detailed tutorial on installing mysql under Linux

1. Shut down the mysql service # service mysqld s...

How to implement page screenshot function in JS

"Page screenshot" is a requirement ofte...

Teach you about react routing in five minutes

Table of contents What is Routing Basic use of pu...

Example of automatic import method of vue3.0 common components

1. Prerequisites We use the require.context metho...

Detailed explanation of the pitfalls of Apache domain name configuration

I have never used apache. After I started working...

Teach you how to install docker on windows 10 home edition

When I wrote the Redis book and the Spring Cloud ...

Oracle VM VirtualBox installation of CentOS7 operating system tutorial diagram

Table of contents Installation Steps Environment ...