HiveBrain v1.2.0
Get Started
← Back to all entries
snippetbashTip

readarray — Read lines from `stdin` into an array. More information: <https://www.gnu.org/software/bash/manual/b

Submitted by: @import:tldr-pages··
0
Viewed 0 times
commandlinesfromreadclistdinintoreadarray

Problem

How to use the readarray command: Read lines from stdin into an array. More information: <https://www.gnu.org/software/bash/manual/bash.html#index-readarray>.

Solution

readarray — Read lines from stdin into an array. More information: <https://www.gnu.org/software/bash/manual/bash.html#index-readarray>.

Interactively input lines into an array:
readarray {{array_name}}


Read lines from a file and insert them in an array:
readarray < {{path/to/file.txt}} {{array_name}}


Remove [t]railing deliminators (newline by default):
readarray < {{path/to/file.txt}} -t {{array_name}}


Copy at most n lines:
readarray < {{path/to/file.txt}} -n {{n}} {{array_name}}


[s]kip the first n lines:
readarray < {{path/to/file.txt}} -s {{n}} {{array_name}}


Define a custom [d]elimiter:
readarray < {{path/to/file.txt}} -d {{delimiter}} {{array_name}}


Display help:
help mapfile

Code Snippets

Interactively input lines into an array

readarray {{array_name}}

Read lines from a file and insert them in an array

readarray < {{path/to/file.txt}} {{array_name}}

Remove [t]railing deliminators (newline by default)

readarray < {{path/to/file.txt}} -t {{array_name}}

Copy at most `n` lines

readarray < {{path/to/file.txt}} -n {{n}} {{array_name}}

[s]kip the first `n` lines

readarray < {{path/to/file.txt}} -s {{n}} {{array_name}}

Context

tldr-pages: common/readarray

Revisions (0)

No revisions yet.