JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Append new data to an array without specifying the index in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

Arrays are a common part of any programming language. In Bash scripts, you can also use arrays; you can declare, modify, and manipulate arrays.

But in this article, we will see step by step how to declare an array and add new data to it. We will see two different ways of adding new data in an array.

Furthermore, we will see necessary examples and explanations to make the topic easier to understand.


Declaring an array in Bash

This is the first step. In this step, we will see how to declare an array in Bash.

The general syntax for declaring an array is:

ArrayName=('Data1' 'Data2' 'Data3')

Below, we declare an empty array with the following code:

MyArray=()

Use the += operator in Bash to append data to an array without specifying an index

We have already created an array, so now we will enter some data. This section will show how we can append data to an array without an index.

The general syntax for this purpose is:

ArrayName+=('Your Data')

In the following example, we will include three data in the array. The code for our example is:

MyArray=()
MyArray+=('A')
MyArray+=('B')
MyArray+=('C')
echo "Current array elements are: ${MyArray[@]}"

In the above example, we first declared an array, then included the data in the array one by one. Finally, we just displayed all the data in an array.

Now, after executing the above code example, you will get an output as shown below:

Current array elements are: A B C

Alternative way to append data to an array without specifying index in Bash

In this method, we will see another alternative to include data in an array without indexing. In the following example, we will include some data in an array without indexing.

The code for our example is as follows:

MyArray=('A' 'B' 'C')
MyArray=(${MyArray[@]} 'D')
MyArray=(${MyArray[@]} 'E')
MyArray=(${MyArray[@]} 'F' 'G')
echo "Current array elements are: ${MyArray[@]}"

In the example, we MyArray=('A' 'B' 'C')declared an array with some data via the line . After that, we included all the data one by one.

Notice that in ${MyArray[@]}the section we append all the previous data to the new data. This is how we repeatedly declare arrays and update data.

Now after executing the above code sample. You will get the following output:

Current array elements are: A B C D E F G

The two methods discussed above are the most reliable ways to append data to an array without indexing.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial