LinuxCommandLibrary

readarray

Read lines from input into an array

TLDR

Interactively input lines into an array

$ readarray [array_name]
copy

Read lines from a file and insert them in an array
$ readarray [array_name] < [path/to/file.txt]
copy

Remove trailing deliminators (newline by default)
$ readarray -t [array_name] < [path/to/file.txt]
copy

Copy at most n lines
$ readarray -n [n] [array_name] < [path/to/file.txt]
copy

Display help
$ help mapfile
copy

SYNOPSIS

readarray [-d delim] [-n count] [-O origin] [-s start] [-t] [-u fd] [array_name]

PARAMETERS

-d delim
    Specifies a delimiter character for lines. If delim is a null string, the line is terminated by the first null character rather than newline.

-n count
    Reads at most count lines. If count is 0, all lines are read.

-O origin
    Starts assigning to array_name at index origin. Defaults to 0. If origin is less than 0, it is treated as 0.

-s start
    Skips the first start lines read from the input. If start is less than 0, it is treated as 0.

-t
    Removes the trailing newline character from each line read. This is useful for preventing empty last elements or unwanted newlines.

-u fd
    Reads lines from the file descriptor fd instead of standard input. If fd is omitted, 0 (stdin) is used.

array_name
    The name of the indexed array variable to be populated. If omitted, the default array MAPFILE is used.

DESCRIPTION

readarray, also known as mapfile, is a powerful Bash built-in command designed to read lines from standard input or a specified file descriptor directly into an indexed array variable. Each complete line read becomes a distinct element in the array. This command offers a highly efficient alternative to traditional line-by-line processing loops, such as while read, especially when dealing with large volumes of data.

It provides options to control how lines are read, allowing users to skip a specified number of initial lines, limit the total number of lines read, or remove trailing newline characters from each element. Furthermore, it enables the specification of the array's starting index, which can be useful for advanced array manipulation. By simplifying the process of ingesting data into arrays, readarray streamlines scripting tasks that involve list processing, configuration file parsing, or log analysis.

CAVEATS

It is a Bash built-in command, not a standalone executable found in /bin or /usr/bin.

readarray is an alias for the mapfile built-in command in Bash. Their functionalities are identical.

Primarily designed for indexed arrays; it does not directly support populating associative arrays.

Behavior and available options may vary slightly across different Bash versions. Ensure your Bash version supports the desired options.

EFFICIENCY

readarray (or mapfile) is significantly faster than a while read loop for reading large files into an array. This performance gain stems from its nature as a compiled Bash built-in, which optimizes the input/output operations.

DEFAULT ARRAY VARIABLE

If no array_name is specified, readarray populates a default array variable named MAPFILE. This can be directly accessed like any other array, e.g., ${MAPFILE[0]}.

COMMON USE CASES

readarray is commonly used for tasks such as parsing configuration files, processing line-oriented log data, handling lists of items from external commands, and general data manipulation where each line represents a distinct record.

HISTORY

The mapfile built-in command was introduced in Bash 4.0 as a more efficient method for reading data into arrays than traditional loops. The readarray alias was subsequently introduced in Bash 4.3 (though some sources mention 4.2), providing a more intuitive and memorable name for the same functionality. Its development aimed to optimize input processing for large files and datasets, addressing performance bottlenecks encountered with older scripting idioms.

SEE ALSO

read(1), cat(1), awk(1), grep(1), sort(1)

Copied to clipboard