Use the mapfile
command:
mapfile -t myArray < file.txt
The error is using for
— the idiomatic way to loop over lines of a file is:
while IFS= read -r line; do echo ">>$line<<"; done < file.txt
See BashFAQ/005 for more details.
More Related Contents:
- Iterate over two arrays simultaneously in bash
- Bash array with spaces in elements
- How to keep associative array order?
- How to solve while loop in array?
- Loop through an array of strings in Bash?
- Check if a Bash array contains a value
- Reading output of a command into an array in Bash
- How can I store the “find” command results as an array in Bash
- How can I join elements of an array in Bash?
- Passing arrays as parameters in bash
- How do I do multiple assignment in MATLAB?
- Capturing output of find . -print0 into a bash array
- Read lines from a file into a Bash array [duplicate]
- How to slice an array in Bash
- How to iterate over array of objects in Handlebars?
- How to return an array in bash without using globals?
- Accessing a JSON object in Bash – associative array / list / another model
- Print array elements on separate lines in Bash?
- Reading a space-delimited string into an array in Bash
- Bash indirect array addressing?
- Convert command line arguments into an array in Bash
- Read tab-separated file line into array
- How to check if element exists in array with jq
- How to pass an array argument to the Bash script
- Assign to a bash array variable indirectly, by dynamically constructed variable name
- How to merge two arrays in a zipper like fashion in Bash?
- How to find the length of an array in shell?
- Check if an element is present in a Bash array [duplicate]
- Is indexing vectors in MATLAB inefficient?
- How to copy an array in Bash?