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