readarray - Linux


Overview

readarray, also known as mapfile, is a builtin command of the Bash shell. It reads lines from a standard input into an array variable. The primary function of readarray is to simplify the process of reading and manipulating multiple lines of input data directly into an array. This command is particularly useful in shell scripting when you need to process or iterate over multiple lines or string elements efficiently.

Syntax

The readarray command syntax is as follows:

readarray [options] array_name

Where array_name is the name of the array variable into which lines of input will be read. Options can be used to modify the behavior of the command.

Options/Flags

Here are the main options and flags available for readarray:

  • -d DELIMITER: Specify a delimiter with which to terminate lines (instead of newline).
  • -n COUNT: Copy at most COUNT lines into the array.
  • -O ORIGIN: Begin storing lines into the array at index ORIGIN.
  • -s COUNT: Discard the first COUNT lines read.
  • -t: Remove the trailing newline from each line read.
  • -u FD: Read input from File Descriptor FD.

Each option adjusts how readarray reads input and stores it into the defined array, facilitating various data manipulation tasks directly from the command line.

Examples

Simple Example:

readarray my_array < myfile.txt

This command reads lines from myfile.txt into my_array.

Using a custom delimiter:

readarray -d ',' my_array < input.csv

This reads input.csv using commas as delimiters instead of newlines.

Limit the number of lines read:

readarray -n 10 my_array < myfile.txt

This only reads the first 10 lines of myfile.txt into the array.

Common Issues

  • Delimiters not working: Ensure the -d flag character is correctly specified. If your delimiter is a special character, it may need to be escaped or quoted.
  • Array index errors: When using -O, ensure that the array is indexed considering Bash starts at index 0. Misindexing can lead to out-of-range errors.
  • File descriptor confusion: When using -u, ensure the correct file descriptor is targeted, avoiding conflicts with standard input/output streams.

Integration

Combining with sort:

readarray -t my_array < <(sort myfile.txt)

This command sorts myfile.txt before reading it into an array, demonstrating how readarray can be piped with other commands.

Script usage example:

#!/bin/bash

# Read file into array
readarray my_data < data.txt

# Process each element
for line in "${my_data[@]}"; do
    echo "Processing: $line"
done

This script reads lines from data.txt into an array and processes each line individually.

  • printf: Useful for formatting array elements when combined with readarray.
  • sort: Often piped before readarray to sort data prior to array storage.
  • echo: Can be used to output array elements.

For further reading and more detailed information, refer to the Bash manual or the online Bash reference documentation which provides comprehensive details on readarray and its usage in script creation.