Creating an array from a text file in Bash

read text file as array bash
bash readarray
bash array
bash read file into array with spaces
bash read file into associative array
bash read column from file into array
bash array of strings
bash readarray from file

A script takes a URL, parses it for the required fields, and redirects its output to be saved in a file, file.txt. The output is saved on a new line each time a field has been found.

file.txt
A Cat
A Dog
A Mouse 
etc... 

I want to take file.txt and create an array from it in a new script, where every line gets to be its own string variable in the array. So far I have tried:

#!/bin/bash

filename=file.txt
declare -a myArray
myArray=(`cat "$filename"`)

for (( i = 0 ; i < 9 ; i++))
do
  echo "Element [$i]: ${myArray[$i]}"
done

When I run this script, whitespace results in words getting split and instead of getting

Desired output
Element [0]: A Cat 
Element [1]: A Dog 
etc... 

I end up getting this:

Actual output
Element [0]: A 
Element [1]: Cat 
Element [2]: A
Element [3]: Dog 
etc... 

How can I adjust the loop below such that the entire string on each line will correspond one-to-one with each variable in the array?

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.

Read lines from a file into a Bash array, Latest revision based on comment from BinaryZebra's comment and tested here. The addition of command eval allows for the expression to be kept in the  Creating an array from a text file in Bash (4) Use mapfile or read -a Always check your code using shellcheck. It will often give you the correct answer.

BASH reading txt file and storing in array, IFS=$'\n' a=($(cat phonebook.txt)) for i in $(seq ${#a[*]}); do [[ ${a[$i-1]} = $name ]] && echo "${a[$i]}" done. In Bash 4 IFS=$'\n' a=($(cat  Creating an array from a text file in Bash. A script takes a URL, parses it for the required fields, and redirects its output to be saved in a file, file.txt. The output is saved on a new line each time a field has been found.

You can do this too:

oldIFS="$IFS"
IFS=$'\n' arr=($(<file))
IFS="$oldIFS"
echo "${arr[1]}" # It will print `A Dog`.

Note:

Filename expansion still occurs. For example, if there's a line with a literal * it will expand to all the files in current folder. So use it only if your file is free of this kind of scenario.

bash: reading a file into an array, bash 4 introduced readarray (also known as mapfile ) which allows you to do: The IFS variable is a string of characters that define how  I have a directory myDir of many .html files. I am trying to create an array of all the files in the directory so I might be able to index the array and be able to refer to particular html files

You can simply read each line from the file and assign it to an array.

#!/bin/bash
i=0
while read line 
do
        arr[$i]="$line"
        i=$((i+1))
done < file.txt

Bash, Bash Read lines of a file into an array. Example#. readarray -t arr <file. Or with a loop: arr=() while IFS= read -r line; do arr+=("$line") done <file. PDF - Download  You did not create an array. What you did was Command Substitution which would simply put the output of a command into a variable. In order to create an array, say: temp_list=( $(grep "[a-z]" failedfiles.txt) ) You might also want to refer to Guide on Arrays.

Use mapfile or read -a

Always check your code using shellcheck. It will often give you the correct answer. In this case SC2207 covers reading a file that either has space separated or newline separated values into an array.

Don't do this
array=( $(mycommand) )
Files with values separated by newlines
mapfile -t array < <(mycommand)
Files with values separated by spaces
IFS=" " read -r -a array <<< "$(mycommand)"

The shellcheck page will give you the rationale why this is considered best practice.

How to read all lines of a file into a bash array, #!/bin/bash declare -a myarray # Load file into array. let i=0 while IFS=$'\n' By default, the bash shell breaks up text into chunks by separating  Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands. Bash is available by default on Linux and macOS operating systems.

Read file into Bash array: [Auto] Bash, Read file into Bash array. If you have a text file that you want to read into an array, you can take each individual line and put them into an #declare your array Following is the simplest method of creating an array variable. This helps assign a value to one of its indices. array_name[index]=value Here array_name is the name of the array, index is the index of the item in the array that you want to set, and value is the value you want to set for that item. As an example, the following commands −

Write array contents to file, Hi, I have a bash script that currently holds some data. I am trying to write all temp.txt # clear file or create for j in ${array[@]} do echo $j >>temp.txt done. Code​:. Create file in Linux command line. There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. 1. Create an empty file using touch command. One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple. touch filename. If the file doesn’t exist

Introduction to Bash arrays, Enter the weird, wondrous world of Bash arrays. The first thing we'll do is define an array containing the values of the Not including brackets, e.g., echo $​allThreads[1] , leads Bash to treat [1] as a string and output it as such. In this scenario, your app is divided into modules, each with its own log file.

Comments
  • This is what Bash FAQ 001 is all about. Also this section of the array topic in Bash FAQ 005.
  • I would link this as a duplicate of stackoverflow.com/questions/11393817/…, but the accepted answer there is awful.
  • Etan, thank you so much for such a fast and accurate reply! I had tried to search my question in the forums, but did not think to look for the FAQ on stackoverflow. The mapfile command addressed my needs exactly! Thanks again :) Answer in section 2.1.
  • (Set up the link in the opposite direction, since we have a better accepted answer here than we have there).
  • Since this is being promoted as the canonical q&a, you could also include what is mentioned in the link: while IFS= read -r; do lines+=("$REPLY"); done <file.
  • mapfile does not exist in bash versions prior to 4.x
  • Bash 4 is about 5 years old now. Upgrade.
  • Despite bash 4 being released in 2009, @ericslaw's comment remains relevant because many machines still ship with bash 3.x (and will not upgrade, so long as bash is released under GPLv3). If you're interested in portability, it's an important thing to note
  • the issue isn't that a developer can't install an upgraded version, it's that a developer should be aware that a script using mapfile will not run as expected on many machines without additional steps. @ericslaw macs will continue to ship with bash 3.2.57 for the foreseeable future. More recent versions use a license that would require apple to share or allow things they don't want to share or allow.
  • I find it that I have to put parentheses around IFS= read -r line || [[ "$line" ]] for it to work. Otherwise, it works great!
  • @TatianaRacheva: isn't it that the semicolon that was missing before do?
  • Is there any way to set IFS only temporarily (so that it recovers its original value after this command), while still persisting the assignment to arr?