mapfile

shell builtinLinux/Unix (Bash)
The mapfile command is one of the most frequently used commands in Linux/Unix-like operating systems. mapfile Read lines from standard input into an indexed array variable

Quick Reference

Command Name:

mapfile

Category:

shell builtin

Platform:

Linux/Unix (Bash)

Basic Usage:

mapfile [options] [arguments]

Common Use Cases

    Syntax

    mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]

    Options

    Option Description
    -d delim Use delim to terminate lines, instead of newline
    -n count Copy at most count lines. If count is 0, all lines are copied
    -O origin Begin assigning to array at index origin. Default index is 0
    -s count Discard the first count lines read
    -t Remove a trailing delim (newline by default) from each line read
    -u fd Read lines from file descriptor fd instead of standard input
    -C callback Evaluate callback each time quantum lines are read
    -c quantum Specify the number of lines read between each call to callback
    array Array variable name to use for file data. If omitted, MAPFILE is used

    Examples

    How to Use These Examples

    The examples below show common ways to use the mapfile command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    mapfile my_array < file.txt
    Read all lines from file.txt into the array my_array.
    mapfile -t my_array < file.txt
    Read lines from file.txt into my_array, removing trailing newlines.
    # Advanced Examples Advanced
    mapfile -n 5 first_five < file.txt Read only the first 5 lines from file.txt. mapfile -s 2 -t -n 3 my_array < file.txt Skip the first 2 lines, then read 3 lines, removing trailing newlines. mapfile -d ":" my_array < /etc/passwd Use colon as the delimiter instead of newline. mapfile -O 10 my_array < file.txt Start populating the array at index 10.
    cat file.txt | mapfile -t lines && echo "Read ${#lines[@]} lines"
    Count the number of lines in a file. mapfile -c 5 -C 'echo "Read 5 more lines..."' my_array < large_file.txt Display progress message after reading each batch of 5 lines. mapfile -u 3 my_array 3< file.txt Read from file descriptor 3 instead of standard input.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'mapfile' command (also known as 'readarray') is a built-in Bash command that efficiently reads lines from standard input or a file descriptor into an indexed array variable. It was introduced in Bash version 4.0 and provides a more efficient and feature-rich alternative to traditional line-by-line reading loops. Key features of the mapfile command: 1. Efficient Line Reading: mapfile reads lines directly into an array without the overhead of a loop, making it faster for processing large files. 2. Array Population Control: It offers options to control which array indices are populated, allowing you to skip lines, limit the number of lines read, or start populating at a specific array index. 3. Trailing Delimiter Handling: With the -t option, mapfile can automatically remove the trailing delimiter (newline by default) from each line, saving an extra step in processing. 4. Custom Delimiters: While mapfile defaults to reading newline-separated lines, it can be configured to use any character as a line delimiter with the -d option. 5. Progress Feedback: For processing large files, mapfile can execute a callback function after reading a specified number of lines, allowing for progress reporting or incremental processing. 6. File Descriptor Support: mapfile can read from any file descriptor, not just standard input, providing flexibility in how data is sourced. 7. Variable Naming: By default, mapfile populates an array named MAPFILE, but you can specify any array name as an argument. Common use cases for mapfile include: - Reading configuration files into arrays for processing - Processing log files line by line - Parsing comma-separated or tab-separated data files - Building arrays of file names or other system information - Creating lookup tables from text data - Efficiently processing large text files without using loops The mapfile command is particularly useful in Bash scripts that need to process text files efficiently. By loading all lines into an array at once, it allows for random access to any line and enables more complex data manipulation than sequential reading. It's worth noting that mapfile is functionally identical to the readarray command in Bash; they are two names for the same built-in command. The command has been available since Bash 4.0, so it may not be available in older Bash versions or in other shells.

    Related Commands

    These commands are frequently used alongside mapfile or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mapfile command works in different scenarios.

    $ mapfile
    View All Commands