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.
Arrays can also be created by assigning elements.
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.
Use @ or * instead of a specific index number to print all elements of an array.
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.
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:
View the array elements after adding new:
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.
View array elements after adding a new element:
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.
View array elements after adding a new element:
You may also be interested in:
|
<<: MySQL database development specifications [recommended]
>>: Understanding and using React useEffect
Table of contents Technology Stack Backend build ...
This article example shares the specific code of ...
This article uses examples to illustrate the opti...
There are significant differences between centos7...
This article describes how to configure time sync...
Now most projects have begun to be deployed on Do...
This article is a self-written imitation of the X...
1. When designing a web page, determining the widt...
Today, due to project requirements, js is needed t...
In order to efficiently meet requirements and avo...
There are 4 commonly used methods, as follows: 1....
1. Background Buttons are very commonly used, and...
Table of contents A. Docker deployment of springb...
Table of contents 1. Select All 2. Increase or de...
The front-end development department is growing, ...