readarray

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

Quick Reference

Command Name:

readarray

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

readarray [options] [arguments]

Common Use Cases

    Syntax

    readarray [options] array

    Options

    Option Description
    -c count Display a line count when reading every COUNT lines
    -C callback Evaluate CALLBACK each time COUNT lines are read
    -d delim Use DELIM to terminate lines, instead of newline
    -n count Read at most COUNT lines
    -O origin Begin assigning to array at index ORIGIN (default is 0)
    -s count Skip the first COUNT lines read
    -t Remove trailing delimiter (newline by default) from each line read
    -u fd Read from file descriptor FD instead of standard input
    -z Line ends with NULL character, not newline

    Examples

    How to Use These Examples

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

    # Basic Examples Basic
    readarray my_array
    Read lines from standard input into array my_array.
    cat file.txt | readarray lines
    Pipe the contents of file.txt into the array 'lines'.
    # Advanced Examples Advanced
    readarray -t my_array Read lines into my_array, removing trailing newlines. readarray -n 5 first_five Read only the first 5 lines into the array. readarray -s 2 skip_two Skip the first 2 lines and read the rest into the array. readarray -O 3 -t my_array Start storing input at index 3 of the array, removing trailing newlines. readarray -t -c 1 my_array Read lines, removing trailing newlines, and display a progress counter. readarray -u 3 file_descriptor Read from file descriptor 3 into an array. exec 3< file.txt readarray -u 3 lines exec 3>&- Read from file.txt using a file descriptor. readarray -t DIRS < <(find /path -type d) Use process substitution to read directory names into an array.

    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 `readarray` command (also known as `mapfile`) is a built-in shell command in Bash that reads lines from standard input or a specified file descriptor into an indexed array variable. It provides a convenient and efficient way to process multi-line input in shell scripts. Introduced in Bash version 4.0, `readarray` offers a more streamlined approach for reading lines into arrays compared to traditional methods using loops with the `read` command. It's particularly useful when working with file content, command output, or any text that needs to be processed line by line. Key features and advantages of the `readarray` command include: 1. Efficiency: `readarray` is more efficient than using loops with `read` for filling arrays, as it's implemented as a shell built-in and optimized for array operations. 2. Line Manipulation: Options like `-t` allow for automatically removing trailing delimiters (like newlines), saving additional processing steps. 3. Selective Reading: The command supports reading only specific portions of input using options like `-n` (maximum lines), `-s` (skip lines), and `-O` (starting index). 4. Progress Monitoring: With the `-c` option, it can display a progress counter when processing large inputs. 5. Custom Delimiters: The `-d` option allows specifying alternative delimiters instead of newlines. 6. Null-Terminated Strings: The `-z` option enables processing null-terminated strings, useful when working with certain command outputs like `find -print0`. Common use cases for `readarray` include: - Reading configuration files into arrays for processing - Capturing and processing command output line by line - Working with file listings or directory contents - Processing CSV or other delimited data - Handling multi-line text inputs in scripts The command is synonymous with `mapfile` in Bash; both names refer to the same functionality. This provides flexibility in script writing, allowing developers to use whichever name they find more intuitive or descriptive for their specific use case. It's worth noting that `readarray` is specific to Bash 4.0 and later, so scripts that use this command may not be portable to environments with older Bash versions or different shells. For maximum portability, traditional loop-based approaches with the `read` command may be preferred.

    Related Commands

    These commands are frequently used alongside readarray 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 readarray command works in different scenarios.

    $ readarray
    View All Commands