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

Creating a Progress Bar in Bash

Publish Date:2025/03/23 Views:94 Category:OPERATING SYSTEM

A progress bar is a visual indicator that shows the progress of a task, such as a long-running script or command. It can be used to provide feedback to the user about the status of a task and can also help estimate the time remaining before

String comparison in batch files

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

A string is an ordered collection of characters. You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batc

Remove double quotes from variable in batch file

Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM

In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa

Reading file into variable in batch script

Publish Date:2025/03/22 Views:121 Category:OPERATING SYSTEM

Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in

Print a file after skipping first X lines in Bash

Publish Date:2025/03/22 Views:187 Category:OPERATING SYSTEM

Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li

Get the Primary IP Address in Linux

Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM

There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:107 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:76 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial