LinuxCommandLibrary

mapfile

Read lines from standard input into array

TLDR

View documentation for the original command

$ tldr readarray
copy

SYNOPSIS

mapfile [-d delim] [-n count] [-N count] [-O origin] [-p prefix] [-q queue] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]

PARAMETERS

-d delim
    Use delim as the line delimiter, rather than newline. If delim is the null string, mapfile will terminate when it reads EOF.

-n count
    Copy only the first count lines.

-N count
    Copy at most count lines. If count is 0, all remaining lines are copied.

-O origin
    Begin assigning to array starting at index origin. The default index is 0.

-p prefix
    Use prefix as a prefix when creating the names of the array. This forces the -C option.

-q queue
    Create a queue of the specified size using the queue discipline.

-s count
    Skip the first count lines.

-t
    Remove a trailing newline character from each line.

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

-C callback
    Evaluate callback after each quantum lines read. The -c option specifies quantum. The default quantum is 5000.

-c quantum
    Specify the number of lines to read before calling the callback function given by the -C option. The default is 5000.

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

DESCRIPTION

The mapfile command in Linux reads lines from standard input (or a specified file) and stores them into a named array variable. Each line becomes an element in the array. This is a convenient way to process files line by line or to work with lists of data within shell scripts.

mapfile offers options for controlling how lines are read, such as specifying a maximum number of lines to read, skipping a number of leading lines, and controlling the handling of whitespace. The default behavior is to read all lines until the end of input. The array created by mapfile can then be manipulated and used within the script. It is especially useful when dealing with text files or output from other commands where line-by-line processing is required.

CAVEATS

mapfile reads lines from input until EOF is encountered, unless the -n option is used to limit the number of lines read. Large files may consume significant memory if read entirely into an array.

If the -u option is not specified, standard input is used as the input file descriptor. The -u option accepts an integer representing a valid file descriptor. If a specified file descriptor doesn't exists, it returns an error.

EXAMPLES

Read all lines from a file into an array:
mapfile -t my_array < my_file.txt
This reads all lines from my_file.txt into the array my_array, removing trailing newlines.

Read the first 10 lines from a file into an array:
mapfile -n 10 my_array < my_file.txt
This reads the first 10 lines.

Skip the first 5 lines and then read the rest into an array:
mapfile -s 5 my_array < my_file.txt

Use a custom delimiter:
mapfile -d ':' my_array <<< "line1:line2:line3"
This command will create the array my_array with elements line1, line2 and line3.

SEE ALSO

read(1), while(1), printf(1)

Copied to clipboard