in Programming by (10.8k points)
0 like 0 dislike

I am trying to read a file containing lines into a bash array. How I can to read lines from a file into a bash array?

bash from file to array
reading a file into an array in bash
read lines of a file into an bash array
reading txt file and storing in array bash

1 Answer

0 like 0 dislike
by (10.8k points)

In bash v4.0 was introduced readarray command (also spelled mapfile). Now reading a file into an array in bash looks like this:

readarray -t arrVar < /path/to/filename

Old solution/approach:

lines=`cat $file`
for line in $lines; do
        echo "$line"

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.