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 [-n count] [-O origin] [-s skip] [-t] [-u fd] [-c quantum] [-C callback] [-d delim] [array]

PARAMETERS

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

-O origin
    Begin assigning to array elements starting with index origin. Default is 0.

-s skip
    Skip the first skip lines before assigning to the array.

-t
    Remove trailing newlines from each line read.

-u fd
    Read from file descriptor fd instead of standard input.

-c quantum
    Invoke callback after reading quantum lines. The callback function is passed the array name as its first argument and the index of the last element as its second argument.

-C callback
    Evaluate callback after reading each line. The callback function is passed the line read as its argument.

-d delim
    End each line with delim, not newline. If delim is an empty string, `readarray` will read until it encounters two consecutive newlines.

array
    The name of the array to store the lines in. If not specified, the default array name is MAPFILE.

DESCRIPTION

The `readarray` command, also known as `mapfile` in Bash 4 and later, reads lines from standard input into an array variable. Each line becomes an element of the array. This is a powerful tool for processing input data, especially when dealing with files where each line represents a distinct piece of information. It allows you to efficiently store and manipulate the data within a script. It offers flexibility in how the input is processed, including specifying a delimiter other than newline, controlling the number of lines read, and assigning a starting index for the array. Using `readarray` can significantly simplify scripts that would otherwise require complex looping and string manipulation to achieve the same result. Consider scenarios such as processing configuration files, parsing command output, or iterating through lists of items where each item resides on a separate line. The array can be indexed and accessed like any other array in bash, allowing for further processing and manipulation of the data.

CAVEATS

Using a large count value without proper memory management can lead to system instability if the input is excessively large.

EXAMPLES

Example 1: Read all lines from a file into an array:
readarray -t my_array < file.txt

Example 2: Skip the first line and read the next 10 lines:
readarray -s 1 -n 10 my_array < file.txt

Example 3: Use a different delimiter:
readarray -d ':' my_array < file.txt

Example 4: Read from file descriptor 3:
exec 3&-

Example 5: Process lines using a callback function:
callback_function() { echo "Processing: $1"; }; readarray -C callback_function my_array < file.txt

DEFAULT ARRAY VARIABLE

If no array name is given as an argument, `readarray` uses the default array variable named `MAPFILE`. This variable is automatically created if it doesn't already exist. Example: readarray < file.txt; echo ${MAPFILE[0]}

HISTORY

The `readarray` command appeared in Bash version 4. Before that, developers used more complex looping constructs to achieve similar functionality, often involving `while` loops and `read`. The introduction of `readarray` (and its alias `mapfile`) significantly simplified these common tasks, leading to cleaner and more efficient shell scripts. It was introduced as a part of enhancements to built-in commands for array manipulations.

SEE ALSO

read(1), printf(1), echo(1)

Copied to clipboard