How to Use readarray Command to Read 2D Array in Bash

How to Use readarray Command to Read 2D Array in Bash

If you have been looking for How to Use readarray Command to Read 2D Array in Bash, here’s the guide for you. 

If you are working with Bash scripts and need to read a 2D array from a file, the “readarray” command can help simplify the process. With “readarray,” you can easily read data from a file into a Bash script and store it in a 2D array, making it easy to manipulate and process the data as needed.

Let’s get started!

What is bash scripting?

Before we proceed to understand how to use readarray command to read a 2D array in bash, let’s take a step back first. Bash is a shell scripting language used in Linux and Unix-based operating systems. There are several built-in commands in shell scripting.

These commands can be used to automate various system-level tasks. These tasks can be performed without scripting, but shell scripting automates these repetitive tasks. Hence, shell scripting can prove to be handy in several situations.

A brief introduction to arrays

An array is a collection of homogeneous data elements that a unique index value can access. It can be used to store and retrieve values quickly. The elements in the array are stored consecutively. 

A one-dimensional array (sometimes called a vector) will have a collection of items (a single row) accessed by a single index value. In two-dimensional arrays (also called a matrix). The individual items are accessed by specifying two indexes. 

readarray command in bash

The readarray command is a handy feature in bash. It was introduced in Bash ver. 4. With this command. You can read lines from an input file or other sources into a 2D array. 

How to Use readarray Command to Read 2D Array in Bash

There are several options that you can specify with the readarray command apart from learning it before moving the discussion, we will discuss the syntax/ usage of the readarray command.

How to Use readarray Command to Read 2D Array in Bash

The general usage of a readarray command is as follows:

readarray [-d DELIM] [-n COUNT] [-O ORIGIN] [-s COUNT] [-t] array

So let’s go ahead and discuss each of these options.

  • Delimiter (-d DELIM) – The option –d is used to specify the delimiter between successive lines while reading the array. By default, the new line character is assumed as the delimiter.
  • Line Count (-n COUNT) – This option is used to specify the number of lines to be read into the array.
  • Origin (-O ORIGIN) – This option sets the array’s starting index.
  • Skip count (-s COUNT) – This option specifies the number of lines to be initially skipped before reading the data into the array from fie.
  • Trailing newline character (-t) – This option removes the trailing newline character when a line is read into the array.
  • Other options  – Besides being discussed above, several alternatives can be used with the readarray command. This includes –u to specify a file descriptor, -C to specify a callback each time quantum lines are read, and –c to specify the number of lines reads between each call to Call back.

Example of readarray

Let’s discuss a few examples of readarray commands. For instance, we have a file input.txt with the following contents:

1 2 3
4 5 6
7 8 9

Example 1: Reading an array from a file using readarray

The next line of code can be used to read the array from the above file:

$ readarray myarray <input.txt   

You can display the content of the whole array:

$ echo "${myarray[@]}"

If you want the content of the first line, you can write:

$ echo ${arr[0]}

Note: Using echo $myarray will only display the first line of the array like echo ${myarray[1]}

Example 2: A detailed example of reading from a file

Now, let’s understand the details by delving into more code. The following line of code can be used to read the array in bash using the readarray command:

How to Use readarray Command to Read 2D Array in Bash

In the above code, line 3 reads the array from input.txt using the readarray command. On line 5, an array is declared. 

Note that to create an array in shell scripting, we use the declare command followed by the name of the array. 

In line 7, we iterate over the line array. In line 8, each input line is split into elements using IFS and read command. 

Note: IFS is internal field separator command. By default, it assumes blank space, tab, and new line characters as separators. It allows the reading function to delimit array items.

In line 11, the data is stored in the array. Finally, line 15-21 prints the 2D array. Assuming you have saved the, you can run the program using the following command:

How to Use readarray Command to Read 2D Array in Bash

Example 3- Reading array from other sources using readarray

Besides the file source, the array can be read from other sources. The following example illustrates it:

$ readarray -t my_array < <(seq 5)
$ declare -p my_array

Here, we have used the seq command to generate numbers from 1 – 5. Then, they are saved into the array.

Note: The bash also has a read command that can read a line from standard input and split it into words.

By learning how to use readarray Command to read 2D Array in Bash, you can specify the delimiter, line count, skip count, origin, and trailing newline character while reading the array.

You can also read the array from different sources, such as files or standard input. By automating repetitive tasks, shell scripting using bash can prove to be a handy tool in various system-level tasks.

If this guide helped you, please share it. 

Related Posts