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

How to use Docker+DockerCompose to encapsulate web applications

Table of contents Technology Stack Backend build ...

Native js custom right-click menu

This article example shares the specific code of ...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

JavaScript imitates Xiaomi carousel effect

This article is a self-written imitation of the X...

Optimal web page width and its compatible implementation method

1. When designing a web page, determining the widt...

Vue large screen data display example

In order to efficiently meet requirements and avo...

Detailed explanation of the use of Element el-button button component

1. Background Buttons are very commonly used, and...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...

Web front-end development CSS related team collaboration

The front-end development department is growing, ...